Indentation is used to organize code with Python. It not only makes the code easier to read (for the programmer) but is actually necessary for the interpreter program to understand what it is reading. Once the logic breaks down, the program breaks down.
So indentation divides code - basically different kinds of statements and definitions - or "commands" if you like - into "blocks". Blocks of code. Why? Because if you define something, then you need to tell the Python interpreter what lines are part of the definition. Because
all of the code might
not be.
The same goes for some types of statements, like if statements or different types of iteration statements. The important thing to know for a "merger" is only that every time a line of code ends with a colon, there is supposed to be new block of code following. And a new block mean that the indentation level goes up - one level.
This brings us to indentation levels. The first level - also known as __main__ - has no indentation. The next level is intended - but by how much? This is actually arbitrary, and this very fact would probably be a major cause for headache for any merger! Because one level can be represented by any number of "whitespace".
First rule: The amount of whitespace must be the same for the entire .py file (or "module"). Second rule: There are two types of whitespace commonly used for indentation; blank spaces and tabs. Its the number of whitespaces used that counts - not how many spaces they take up or how it looks in any given text/code editor!
So you basically can't mix and match blank space and tabs. Figure out what type of whitespace is used in the module your work is based upon - and how many whitespace of this type is used. Then you have to convert any code that you're importing (copy-pasting into the script or program) so that the indentation levels match - both with regard to what type of and how many whitespace is being used - and what indentation level the code belongs to.
This brings us back to the topic at hand: Indentation levels. Example of a class definition:
Class definitions are mostly found at the first level, so there would be no indentation. Add a function (or actually method, but this is not important) definition:
Code:
def __init__(self, setting):
Three things to note right here:
- Since the first line (the class definition) ended with a colon, there should be a new block of code. This is achieved with indentation.
- Now is when I decide what whitespace I'm gonna use and how many instances of it. In this example I went with four times blank space - from now on this is the standard.
- This line also ends with a colon - meaning that there should be another block of code:
This assignment statement is intended eight blank spaces and is thus a valid block of code (2 * 4). Since there never is a colon at the end of any assignment statement, the next line of code belongs to the same block. So it gets the same amount of indentation:
Opps, there is that colon again! An if statement always has to be followed by a new block of code:
Now, for the sake of computer logic, we want to return to the previous indentation level. So we subtract four instances of blank space from the previous indentation level:
Another colon...
If we wanna define another method belonging to the same class - or level - as before we have to back up to the same indentation level as the first one (1 x 4):
And so on... This is what our Python script could look like:
All this would of course only be obvious for a programmer - and it would pretty much take a Python programmer to realize the finer points of the use of indentation. But it would none-the-less be required knowledge for anyone trying to merge Python code!
(Blank lines mean nothing to the Python interpreter, by the way. This is really only a kind of typography I'm using to illustrate my example in a clearer manner. You can add or subtract as many blank lines you want!)
Take a look at some sample code you are working with - or have worked with in the past - and try to see the logic behind the various levels and blocks of code. You don't need to understand what any single line of code means or does, but you will see that it all adheres to the simple rules stated in this post.
Next time you merge any Python code, start with figuring out what the indentation levels are and how they are represented. Then do your out-most to follow this example in the entire file! (But be prepared that what you just did doens't necessarily apply in another Python module. You actually save time by checking this as your first order of business.)
edit: Another rule you should know about: The indentation level can
never go up more than one level at the time! (It can drop any number of levels though, adhering to the rules of logic.)