indendation question

Robin Munn rmunn at pobox.com
Mon Dec 29 10:01:51 EST 2003


Stephen Ferg <steve at ferg.org> wrote:
> The official recommendation is to use 4 spaces, but...
>
> ... Using tabs seems much more natural to me.  
>
> One level of indentation == one tab character.  
> What could be more natural?
>
> So I always use tabs, and that's what I recommend.

The problem with tab characters is that it makes it very difficult to
share your code with others. Everyone has different settings for their
tab stops: some use 4, most use 8, a few heretics :-) use 3... Thus,
your code, that you carefully arranged to be less than 80 characters
wide, will someday be read by someone who has 8-space tabs (while you
use 4-space tabs), and on his screen, it will wrap around most
annoyingly.

And if someone else is *editing* the same file as you, it will make
matters even worse, because they're likely to be using spaces for
indentation. So you've indented something with one tab, which on their
screen looks like 8 spaces. They add a line at what they believe to be
the same level of indentation: 8 spaces. Then you look at the file and
see their line as being one step more indented than yours. Now try to
guess what Python will do with that file.

This isn't just a theoretical problem: I've personally cleaned up files
(although they weren't written in Python) that had had exactly this
happen to them. That's why I recommend space characters, not tabs. Yes,
it may feel more natural to just hit the Tab key to indent -- but any
decent programming editor can be set up to insert the right number of
spaces when you hit the Tab key! In vim, for instance, look at the
"shiftwidth" and "smarttab" options.

On the other hand, read the rationale for PEP 666:

    http://www.python.org/peps/pep-0666.html

-- 
Robin Munn
rmunn at pobox.com




More information about the Python-list mailing list