Python def no indentation

There are several occurrences of the following format (note that there is no indentation after the def lines) in the original file I have:

def sendSystemParms2(self, destAddress, subid, state):

raw = sysparms_format2.build(Container(
length = 0x0D,
command = MT_APP_MSG_CMD0))

def method2(parm2):
print parm2

      

and it works and works. I'm confused. Any hints? I am unable to upload the image due to the fact that it was new and did not have sufficient reputation, but I can show proof.

+3


source to share


1 answer


You have a file that uses a mixture of tabs and spaces.

Python expands tabs to eight spaces, but you're looking at a file in an editor that uses a tab size of four spaces.

Function bodies use tabs for indentation, but lines def

use 4 spaces instead. As with Python, method bodies are indented correctly.

If I set my text editor to use 8-space tabs and then select text to highlight the editor selection tabs, I see:

source code with indentation highlighted



The lines indicate the tabs.

This is one of the reasons why you shouldn't use tabs for indentation. The Python style guide recommends using only spaces for indentation. In Python 3, mixing tabs and spaces like this is a syntax error.

You can tell Python 2 to pick up a TabError

combination of tabs and spaces for this by starting Python with the -tt

switch command line
:

-t


Issue a warning when the source file mixes tabs and indentation spaces in such a way that it depends on the tab's value in spaces. Error if option is given twice (-tt).

+9


source







All Articles