Whitespace! The bane of all aspiring python programmers! After their first day of python programming, it never seems to bother them. Until they get the dreaded file that uses tabs instead of more useful spaced indentations. (I've heard 6 is the new 2. At least that's what they're teaching at MSU).
Emacs (builtin python-mode) allows you to just open their tab delimited files and start working with them just fine. You will follow pep8, and be consistent. No harm done. Allow the infidels to keep their tabs.
Now weird spaced indentation (ala googly 2 space or MSU 6 space), is handled pretty easily as well. Just use M-x set-variable tab-width to the appropriate size. (M-x describe-variable tab-width will show the current value). (A better explanation of tabs/spaces in emacs is found here)
The real problems come when tabs/spaces are intermixed. Out of the box emacs handles that pretty well too. Select a region (C-x h (to select the whole buffer)) and M-x untabify will replace tabs with the tab-width number of spaces. That works pretty well too. (There's also the tabify command for you infidels).
Since some of the fancy gui editors/IDEs out there visually highlight the differing indentation, surely you should be able to replicate this on your emacsen. Alas, emacswiki to the rescue. After an initial hint to look at "blank-mode", it turns out that whitespace-mode is the blank-mode++, (blank-mode was so 2006). Plus documentation found in the wiki is quite nice.
If you have mixed indentation in python, whitespace-mode is handy. I'm finding it even more handy in my html templates that have dynamic css/js/html (where I've yet been able to get an editor to understand what it's supposed to be doing. Yeah, some of it could be isolated to it's own files. Some of it is dynamic and the templating languages further confuse emacs). So when your co-workers might be using Textmate (which might also be confused on such files), tabs might find their way in. whitespace-mode allows one to quickly view and clean up their until now hidden snafus.
The screenshot below illustrates whitespace-mode. In the top buffer is a previously mentioned html template. Red indicates extra space at the end of the line. >> indicates evil tabs. Dots indicate spaces, and $ is newline. You can see that one line has spaces and tabs intermixed! Arghh, my eyes are burning! The middle buffer shows some python code using tabs with whitespace-mode off. The bottom buffer shows the same code with whitespace-mode on.
I'm not going to leave whitespace-mode on all the time. On clean code it's distracting. But when I'm feeling like refactoring/cleanup/examining new code it comes in handy.
Robert-
2 spaces is not a "Python" standard. It's a Google standard for python.
I've heard it said that it's to differentiate between internal/external
code. I'm not sure if that was originally the case, but they could make a
case for it now.
vk0000 -
Yes, good eye! It looks wonderful at 7 pt on my machine.
Nice article, thanks for sharing. On another note, where did you get the
color theme in your screenshot? Is it custom? Or did you pull from
somewhere else?