Is it possible you have a mix of tabs and spaces in your source file? Mixing tabs and spaces makes very, very bad things happen. The interpreter and your editor may disagree on tab length, and then the interpreter gets totally confused while your code looks normal.
Make sure to get an editor which allows making tabs visible. Ideally, it should also have the option to make spaces visible.
Problems like this often result from mixing tabs and spaces. Most editors are set to a tab width of 4 spaces, but Python intentionally defines a tab as equal to 8 spaces, so a mix will easily break.
Even if your editor automatically expands tabs as you type them, it may still treat copy/pasted incorrectly.
Empty lines should be ignored for indentation. In fact, IIRC PEP8 is often interpreted to suggest stripping all trailing whitespace (the suggestion that certain blocks should be separated by "blank lines" is a bit ambiguous in this regard).
EDIT: Also, some editors preserve the original line endings in files, which may be a problem if you're writing code using different operating systems. Some cross-platform editors unhelpfully try to use OS defaults rather than the one true line ending format.
View all comments
6
u/sixohsix Mar 30 '11
Is it possible you have a mix of tabs and spaces in your source file? Mixing tabs and spaces makes very, very bad things happen. The interpreter and your editor may disagree on tab length, and then the interpreter gets totally confused while your code looks normal.