Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok??

John Roth newsgroups at jhrothjr.com
Thu Oct 23 06:38:33 EDT 2003


"Francis Avila" <francisgavila at yahoo.com> wrote in message
news:vpe3ueti7lj3df at corp.supernews.com...
>
> "David C. Fox" <davidcfox at post.harvard.edu> wrote in message
> news:FYDlb.2596$e01.5531 at attbi_s02...
> > In Python, newlines are significant, except within lists, tuples, etc.
> > and a few other cases, so you generally have to escape them with \
> > within statements.  If you use 8-space indents in Python, you very
> > quickly end up having to escape a lot of new lines, which is annoying.
>
>
> As a side note, Python considers a tab to be == 8 spaces.  So you can mix
> spaces and tabs in a single file as long as you use an 8-space convention.
> Now, if your editor displays \t as something other than 8 spaces (I think
8
> is default in almost every editor, though), the indentation can LOOK
wrong,
> even though Python has no trouble with it!

I considered not dropping into this conversation, since Peter Hanson's
reply is absolutely correct - the subject is dead and won't be revisited.
However, there are two errors in the previous paragraph that I think
need to be addressed.

First, Python, along with most xNIX sofware, considers a tab to
be an instruction to move to the next column that is a multiple of 8
(or 8 + 1 if you're starting at 0.)

It's also regarded as ***VERY*** bad practice to mix spaces
and tabs for indentation, and you are guaranteed to get bad results
if you move between editors that have the default number of spaces
to generate for tabs set to something different. In fact, the ability
to use tabs at all for indentation may very well vanish in Python
3.0 (although not sooner.)

> Given that 4 spaces is overwhelmingly dominant in Python code, shouldn't
> that behavior be changed, so that '\t'==' '*4?
>
> Or perhaps python should have a heuristic, where the first first-level
> indent in the file which is indented entirely by spaces establishes the
> number of spaces that a tab character should be equivalent to.

Historically, Python has had the ability to identify the footprint that
a number of popular editors leave in the file. This has never been
documented, and since I don't use emacs, I don't know if it's still
in the current version of Python. It may not be, since it's only useful
if you mix tabs and spaces, and that's been identified as bad practice.
It's also not completely compatible with the character set comment
in 2.3

> (Or maybe it shouldn't, since mixing spaces and tabs is bound to cause
> trouble eventually, and better to find out sooner than later...)

Most editors can be set to generate spaces when you use the
tab key. It's the recommended practice with Python.

John Roth


> --
> Francis Avila
>






More information about the Python-list mailing list