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?

Recommended Answers

All 6 Replies

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!

I think I read something in Guido's Python regrets that he wished he would've enforced that only spaces be used and not allow tabs whatsoever. It's been a while so perhaps I just made that up.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.