I was looking over some code for an open source project and I saw some strange whitespace stuff that I didn't even think would run. The thing is, it runs just fine. I was wondering if someone could explain what I'm missing.

I don't want to copy the exact code, so I'll simplify...
It starts with a class definition, easy enough
class KevinsClass:

the next line is indented four space...
__init__(self, name):

The next line is indented eight spaces....
self.name = name

the next line is indented with one tab
<tab>self.othername = name

I would think that python would bark about the whitespace not being correct. I thought that if you start a block of code, like a class, then every line of that class has to be indented the same way as the first line. So if I indented four spaces, I thought that everything else in the class needed to be indented four spaces.

What am I missing?

The only answer I come up with (and I did get the same result in my sample code) is that tabs equate to 8 characters.

You start the class at the left margin

The init function MUST be indented at least one space, but the actual count is not important, but must be the same for all future methods.

The code inside init must be indented at least one space from the init declaration, but once again the important part is that all lines in the method have the same indent.

I have a sample __init__ method that has 8 spaces for the first self.name = name and one tab for the second self.othername = name

It compiles without errors. If I add a space after the tab it complains about the indenting (like I thought it would have for the tab).

So point 1, no you're not just seeing things. Point 2 is that the default tab expansion MUST be 8 characters, otherwise we would have seen an error.

A cardinal rule is don't mix tabs and spaces for the obvious reason that everyone does not have the same tab settings. The unoffical poll for this forum is that most people use spaces only. Most editors/IDE can be set to indent with either.

I found out that this project has fixed all of the tab/space problems in its code so that isn't going to be a problem. I still don't understand why the code ran in the first place, but I guess it isn't going to be a problem in the future. Thanks

I was looking over some code for an open source project and I saw some strange whitespace stuff that I didn't even think would run. The thing is, it runs just fine. I was wondering if someone could explain what I'm missing.

I don't want to copy the exact code, so I'll simplify...
It starts with a class definition, easy enough
class KevinsClass:

the next line is indented four space...
__init__(self, name):

The next line is indented eight spaces....
self.name = name

the next line is indented with one tab
<tab>self.othername = name

I would think that python would bark about the whitespace not being correct. I thought that if you start a block of code, like a class, then every line of that class has to be indented the same way as the first line. So if I indented four spaces, I thought that everything else in the class needed to be indented four spaces.

What am I missing?

If I would be running this code from my editor, I would have a royal mess on my hand because my editor is set to a tab width of four spaces! You just lucked out with your tab setting. The moral is, for heaven sake don't mix tabs and spaces in your code! Use spaces only!

A lot of editors written with Python in mind automaticaly replace tabs with spaces, but again it depends on the tab settings original versus present.

This article has been dead for over six months. Start a new discussion instead.