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

Discussion in 'Python' started by Christian Seberino, Oct 22, 2003.

  1. Linux kernel style guide, Guido's C style guide and (I believe) old
    K&R style recommends 8 SPACES for indent.

    I finally got convinced of wisdom of 8 space indentation.

    Guido also likes 8 space indentation FOR C CODE.

    Why style guide (PEP-8) for Python says 4 space indents???

    Is breaking rule to use 8 space indents everywhere
    a REALLY bad idea??

    I REALLY WANT TO DO MY OPEN SOURCE PYTHON PROJECT
    WITH 8 SPACE IDENTS!!!!

    Chris
    Christian Seberino, Oct 22, 2003
    #1
    1. Advertising

  2. Christian Seberino

    David C. Fox Guest

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

    Christian Seberino wrote:
    > Linux kernel style guide, Guido's C style guide and (I believe) old
    > K&R style recommends 8 SPACES for indent.
    >
    > I finally got convinced of wisdom of 8 space indentation.
    >
    > Guido also likes 8 space indentation FOR C CODE.
    >
    > Why style guide (PEP-8) for Python says 4 space indents???
    >
    > Is breaking rule to use 8 space indents everywhere
    > a REALLY bad idea??
    >
    > I REALLY WANT TO DO MY OPEN SOURCE PYTHON PROJECT
    > WITH 8 SPACE IDENTS!!!!
    >
    > Chris


    I don't have an official answer, but here are my guesses:

    a)

    In C, new lines are generally like any other white space (except within
    strings, C++ // comments), so you can easily put new lines in the middle
    of an expression.

    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.

    b)

    Unless you have an editor which converts tabs to a fixed number of
    spaces, typing 8 spaces is a pain. Even editors which do convert tabs
    to spaces may not provide an easy way to un-indent by the same number of
    spaces

    --------

    Of course, both these reasons would imply that 2 spaces was better than
    4. Using 4 spaces was probably chosen as a compromise between points a)
    and b) and making sure that the indentation level was clearly visible.

    David
    David C. Fox, Oct 22, 2003
    #2
    1. Advertising

  3. "David C. Fox" <> wrote in message
    news:FYDlb.2596$e01.5531@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!

    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.

    (Or maybe it shouldn't, since mixing spaces and tabs is bound to cause
    trouble eventually, and better to find out sooner than later...)
    --
    Francis Avila
    Francis Avila, Oct 23, 2003
    #3
  4. Re: Why Python style guide (PEP-8) says 4 space indents instead of 8space??? 8 space indents ever ok??

    Christian Seberino wrote:

    > I REALLY WANT TO DO MY OPEN SOURCE PYTHON PROJECT
    > WITH 8 SPACE IDENTS!!!!


    You don't really think a style guide is going to stop you from doing
    that, do you?

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ Golf is a good walk spoiled.
    \__/ Mark Twain
    Erik Max Francis, Oct 23, 2003
    #4
  5. Christian Seberino

    Peter Hansen Guest

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

    Francis Avila wrote:
    >
    > 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.


    Anyone who believes there is or will be anything new in this discussion
    hasn't been reading comp.lang.python or the mailing list nearly long enough.

    Check the archives if you are having trouble falling asleep tonight,
    and keep in mind the utter futility of writing anything more on this
    subject (unless you just like to see yourself in print). :)

    -Peter
    Peter Hansen, Oct 23, 2003
    #5
  6. Christian Seberino

    Stan Graves Guest

    (Christian Seberino) wrote in message news:<>...
    > Why style guide (PEP-8) for Python says 4 space indents???


    Never heard of that. I guess I need to read less about programming
    and more about style. I use 2 space indents. Easy to type, easy to
    change, and I'm never tempted to use tabs instead...

    --Stan Graves

    http://www.SoundInMotionDJ.com
    Stan Graves, Oct 23, 2003
    #6
  7. (Stan Graves) writes:

    > Never heard of that. I guess I need to read less about programming
    > and more about style. I use 2 space indents. Easy to type, easy to
    > change, and I'm never tempted to use tabs instead...


    In non-Python-aware editors (like Usenet messages), I prefer 2-space
    indentation as well. While programming larger projects, I use a
    Python-aware editor, and that editor makes indentation automatically
    (to four spaces), so it is just as easy to type.

    Regards,
    Martin
    Martin v. =?iso-8859-15?q?L=F6wis?=, Oct 23, 2003
    #7
  8. Christian Seberino

    Paul Boddie Guest

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

    Erik Max Francis <> wrote in message news:<>...
    > Christian Seberino wrote:
    >
    > > I REALLY WANT TO DO MY OPEN SOURCE PYTHON PROJECT
    > > WITH 8 SPACE IDENTS!!!!

    >
    > You don't really think a style guide is going to stop you from doing
    > that, do you?


    Keep looking over your shoulder! Only if you expect the (style guide)
    Spanish Inquisition, will they never show up. That's your only
    protection if you're an eight spaces heretic. ;-)

    Paul

    P.S. Seriously, most people respect the indentation of the project
    "lead" on open source projects - for example, Webware employs hard
    tabs throughout its source code, and yet very few people go in and
    mangle the code with two space indents and other such misdemeanours.
    Some people claim some kind of inherent superiority with "whitespace
    insensitive" languages, but I imagine that they are members of that
    irritating club who go into C++ files and trash the indentation,
    claiming that "it looks OK in Visual Studio". I suppose that this is
    another area where Python almost inadvertently encourages more
    considerate programming practices.
    Paul Boddie, Oct 23, 2003
    #8
  9. Christian Seberino

    John Roth Guest

    "Francis Avila" <> wrote in message
    news:...
    >
    > "David C. Fox" <> wrote in message
    > news:FYDlb.2596$e01.5531@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
    >
    John Roth, Oct 23, 2003
    #9
  10. Christian Seberino

    Hung Jung Lu Guest

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

    Peter Hansen <> wrote in message news:<>...
    > Anyone who believes there is or will be anything new in this discussion
    > hasn't been reading comp.lang.python or the mailing list nearly long enough.


    Yeah, the eternal, never-ending war between the tabibans and the tabifans.

    Hung Jung
    Hung Jung Lu, Oct 23, 2003
    #10
  11. Christian Seberino

    Oren Tirosh Guest

    Re: Tabs

    Some tabs are 8 space wide
    But spaces don't cost any money
    Other tabs leap 4 in one stride
    They make my source code look funny
    Oren Tirosh, Oct 23, 2003
    #11
  12. Christian Seberino

    Peter Hansen Guest

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

    Hung Jung Lu wrote:
    >
    > Peter Hansen <> wrote in message news:<>...
    > > Anyone who believes there is or will be anything new in this discussion
    > > hasn't been reading comp.lang.python or the mailing list nearly long enough.

    >
    > Yeah, the eternal, never-ending war between the tabibans and the tabifans.


    Careful. I think you've just attracted the attention of the Bush war machine...

    :)

    -Peter
    Peter Hansen, Oct 24, 2003
    #12
  13. Christian Seberino

    Aahz Guest

    In article <-berlin.de>,
    Martin v. =?iso-8859-15?q?L=F6wis?= <> wrote:
    >
    >In non-Python-aware editors (like Usenet messages), I prefer 2-space
    >indentation as well. While programming larger projects, I use a
    >Python-aware editor, and that editor makes indentation automatically
    >(to four spaces), so it is just as easy to type.


    Get a better newsreader. ;-) (My newsreader lets me pick any editor I
    want.)
    --
    Aahz () <*> http://www.pythoncraft.com/

    "It is easier to optimize correct code than to correct optimized code."
    --Bill Harlan
    Aahz, Oct 24, 2003
    #13
  14. Christian Seberino

    JanC Guest

    (Aahz) schreef:

    > Get a better newsreader. ;-) (My newsreader lets me pick any editor I
    > want.)


    And Martin uses an editor as his newsreader... :p

    --
    JanC

    "Be strict when sending and tolerant when receiving."
    RFC 1958 - Architectural Principles of the Internet - section 3.9
    JanC, Oct 24, 2003
    #14
  15. Re: Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok??

    Peter Hansen <> writes:

    > Hung Jung Lu wrote:
    > >
    > > Peter Hansen <> wrote in message news:<>...
    > > > Anyone who believes there is or will be anything new in this discussion
    > > > hasn't been reading comp.lang.python or the mailing list nearly long enough.

    > >
    > > Yeah, the eternal, never-ending war between the tabibans and the tabifans.

    >
    > Careful. I think you've just attracted the attention of the Bush war machine...


    We have always been at war with FourSpaceTabania!

    Cheers,
    mwh

    --
    "Also, does the simple algorithm you used in Cyclops have a name?"
    "Not officially, but it answers to "hey, dumb-ass!"
    -- Neil Schemenauer and Tim Peters, 23 Feb 2001
    Michael Hudson, Oct 24, 2003
    #15
  16. Re: Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok??

    On Wed, 22 Oct 2003 16:16:15 -0700, Erik Max Francis <>
    wrote:

    >Christian Seberino wrote:
    >
    >> I REALLY WANT TO DO MY OPEN SOURCE PYTHON PROJECT
    >> WITH 8 SPACE IDENTS!!!!

    >
    >You don't really think a style guide is going to stop you from doing
    >that, do you?


    Exactly.

    My advice is basically do what suits you. Just do it consistently.

    Personally, I prefer two space indents. I'm just too lazy to type any
    more if I can avoid it. But when editing existing code, in any
    language, I just go with what's already there - as long as there is
    some consistent indentation rule evident, anyway.


    Are there any tools which can intelligently redo the indentation in a
    Python source file? Preferably respecting other formatting conventions
    where practical, though of course I accept that no program could
    replace a programmer with an eye for readability in this respect.


    --
    Steve Horne

    steve at ninereeds dot fsnet dot co dot uk
    Stephen Horne, Oct 26, 2003
    #16
  17. Christian Seberino

    Dave Kuhlman Guest

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

    Stephen Horne wrote:

    [snip]
    >
    > Are there any tools which can intelligently redo the indentation
    > in a Python source file? Preferably respecting other formatting
    > conventions where practical, though of course I accept that no
    > program could replace a programmer with an eye for readability in
    > this respect.


    If you are on Linux/UNIX or have access to the cygwin tools on MS
    Windows, then try:

    unexpand -4 orig.py > tmp.py
    expand -2 tmp.py > dest.py

    to convert from 4-space indents to 2-space indents, and

    unexpand -2 orig.py > tmp.py
    expand -4 tmp.py > dest.py

    to convert from 2-space indents to 4-space indents.

    But, it does seem like a lot of trouble to go through just so that
    people will look at your code and say "Eewh! Why didn't s/he read
    the style guide?" Or maybe they will smirk and say, "I'll bet s/he
    uses an editor in which the Tab key doesn't work, ... something
    like Notepad on MS Windows. I pity the fool."

    Also, if you have the Python source distribution, look at
    Tools/scripts/untabify.py. Also, in the string module (in the
    standard library) and the string data type, look at the
    expandtabs() method.

    Except, I don't know what these do with tabs in quoted strings.
    The default behavior of unexpand is to convert only
    characters at the beginning of each line. With expand, use the -i
    option to convert only initial tabs.

    Next, ask yourself the following questions: Why is there no
    Tools/scripts/tabify.py? And, why is there no string.unexpandtabs()
    method? Could there be a conspiracy to take away your right to
    use tabs?

    Dave

    --
    Dave Kuhlman
    http://www.rexx.com/~dkuhlman
    Dave Kuhlman, Oct 27, 2003
    #17
  18. Re: Why Python style guide (PEP-8) says 4 space indents instead of 8space??? 8 space indents ever ok??

    Dave Kuhlman wrote:

    > If you are on Linux/UNIX or have access to the cygwin tools on MS
    > Windows, then try:
    >
    > unexpand -4 orig.py > tmp.py
    > expand -2 tmp.py > dest.py
    >
    > to convert from 4-space indents to 2-space indents, and
    >
    > unexpand -2 orig.py > tmp.py
    > expand -4 tmp.py > dest.py
    >
    > to convert from 2-space indents to 4-space indents.


    Blasphemy! Use a pipe and avoid the need for the temporary file
    altogether:

    unexpand -4 orig.py | expand -2 > dest.py

    etc.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ To endure what is unendurable is true endurance.
    \__/ (a Japanese proverb)
    Erik Max Francis, Oct 27, 2003
    #18
  19. Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok??

    Stephen Horne wrote:
    [snip]

    > Are there any tools which can intelligently redo the indentation in a
    > Python source file? Preferably respecting other formatting conventions
    > where practical, though of course I accept that no program could
    > replace a programmer with an eye for readability in this respect.
    >
    >

    ..../Python.../Tools/Scripts/reindent.py
    Steve Williams, Oct 27, 2003
    #19
  20. Christian Seberino

    Harry George Guest

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

    Stephen Horne <> writes:

    > On Wed, 22 Oct 2003 16:16:15 -0700, Erik Max Francis <>
    > wrote:
    >
    > >Christian Seberino wrote:
    > >
    > >> I REALLY WANT TO DO MY OPEN SOURCE PYTHON PROJECT
    > >> WITH 8 SPACE IDENTS!!!!

    > >
    > >You don't really think a style guide is going to stop you from doing
    > >that, do you?

    >
    > Exactly.
    >
    > My advice is basically do what suits you. Just do it consistently.
    >
    > Personally, I prefer two space indents. I'm just too lazy to type any
    > more if I can avoid it. But when editing existing code, in any
    > language, I just go with what's already there - as long as there is
    > some consistent indentation rule evident, anyway.
    >


    If you never work with others, never edit code from others, and never
    offer code to the OSS world, then I suppose you can do your own indent
    rules. But for those of us in the connected world (and esp. those
    doing XP), playing by the 4-char rule is crucial.

    So the question is: How can you do the correct indents everytime,
    without having to manually count them out? You should be using an
    editor which understands python indents (e.g., emacs with
    python-model.el). Even vi and nedit can be set up to understand
    4-char indents.


    >
    > Are there any tools which can intelligently redo the indentation in a
    > Python source file? Preferably respecting other formatting conventions
    > where practical, though of course I accept that no program could
    > replace a programmer with an eye for readability in this respect.
    >



    >
    > --
    > Steve Horne
    >
    > steve at ninereeds dot fsnet dot co dot uk


    --

    6-6M31 Knowledge Management
    Phone: (425) 342-5601
    Harry George, Oct 27, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    416
    Soren Kuula
    May 27, 2005
  2. Ian Bicking
    Replies:
    2
    Views:
    984
    Steve Lamb
    Oct 23, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    709
    Michael Hudson
    Oct 24, 2003
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  5. Jason
    Replies:
    0
    Views:
    187
    Jason
    Jul 6, 2004
Loading...

Share This Page