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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Seberino

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

    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
  • David C. Fox

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

    Christian Seberino wrote:[color=blue]
    > 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[/color]

    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

    Comment

    • Erik Max Francis

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

      Christian Seberino wrote:
      [color=blue]
      > I REALLY WANT TO DO MY OPEN SOURCE PYTHON PROJECT
      > WITH 8 SPACE IDENTS!!!![/color]

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

      --
      Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
      __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
      / \ Golf is a good walk spoiled.
      \__/ Mark Twain

      Comment

      • Francis Avila

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


        "David C. Fox" <davidcfox@post .harvard.edu> wrote in message
        news:FYDlb.2596 $e01.5531@attbi _s02...[color=blue]
        > 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.[/color]


        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

        Comment

        • Peter Hansen

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

          Francis Avila wrote:[color=blue]
          >
          > 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.[/color]

          Anyone who believes there is or will be anything new in this discussion
          hasn't been reading comp.lang.pytho n 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

          Comment

          • Stan Graves

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

            seberino@spawar .navy.mil (Christian Seberino) wrote in message news:<bf23f78f. 0310221431.7f28 24b3@posting.go ogle.com>...[color=blue]
            > Why style guide (PEP-8) for Python says 4 space indents???[/color]

            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
            stan@SoundInMot ionDJ.com

            Comment

            • Martin v. Löwis

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

              soundinmotiondj @yahoo.com (Stan Graves) writes:
              [color=blue]
              > 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...[/color]

              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

              Comment

              • Paul Boddie

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

                Erik Max Francis <max@alcyone.co m> wrote in message news:<3F970FBF. 85974E79@alcyon e.com>...[color=blue]
                > Christian Seberino wrote:
                >[color=green]
                > > I REALLY WANT TO DO MY OPEN SOURCE PYTHON PROJECT
                > > WITH 8 SPACE IDENTS!!!![/color]
                >
                > You don't really think a style guide is going to stop you from doing
                > that, do you?[/color]

                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.

                Comment

                • John Roth

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


                  "Francis Avila" <francisgavila@ yahoo.com> wrote in message
                  news:vpe3ueti7l j3df@corp.super news.com...[color=blue]
                  >
                  > "David C. Fox" <davidcfox@post .harvard.edu> wrote in message
                  > news:FYDlb.2596 $e01.5531@attbi _s02...[color=green]
                  > > 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.[/color]
                  >
                  >
                  > 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[/color]
                  8[color=blue]
                  > is default in almost every editor, though), the indentation can LOOK[/color]
                  wrong,[color=blue]
                  > even though Python has no trouble with it![/color]

                  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.)
                  [color=blue]
                  > 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.[/color]

                  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
                  [color=blue]
                  > (Or maybe it shouldn't, since mixing spaces and tabs is bound to cause
                  > trouble eventually, and better to find out sooner than later...)[/color]

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

                  John Roth

                  [color=blue]
                  > --
                  > Francis Avila
                  >[/color]


                  Comment

                  • Hung Jung Lu

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

                    Peter Hansen <peter@engcorp. com> wrote in message news:<3F971996. 80B30C6D@engcor p.com>...[color=blue]
                    > Anyone who believes there is or will be anything new in this discussion
                    > hasn't been reading comp.lang.pytho n or the mailing list nearly long enough.[/color]

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

                    Hung Jung

                    Comment

                    • Oren Tirosh

                      #11
                      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


                      Comment

                      • Peter Hansen

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

                        Hung Jung Lu wrote:[color=blue]
                        >
                        > Peter Hansen <peter@engcorp. com> wrote in message news:<3F971996. 80B30C6D@engcor p.com>...[color=green]
                        > > Anyone who believes there is or will be anything new in this discussion
                        > > hasn't been reading comp.lang.pytho n or the mailing list nearly long enough.[/color]
                        >
                        > Yeah, the eternal, never-ending war between the tabibans and the tabifans.[/color]

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

                        :-)

                        -Peter

                        Comment

                        • Aahz

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

                          In article <m3ismglb3j.fsf @mira.informati k.hu-berlin.de>,
                          Martin v. =?iso-8859-15?q?L=F6wis?= <martin@v.loewi s.de> wrote:[color=blue]
                          >
                          >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.[/color]

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

                          "It is easier to optimize correct code than to correct optimized code."
                          --Bill Harlan

                          Comment

                          • JanC

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

                            aahz@pythoncraf t.com (Aahz) schreef:
                            [color=blue]
                            > Get a better newsreader. ;-) (My newsreader lets me pick any editor I
                            > want.)[/color]

                            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

                            Comment

                            • Michael Hudson

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

                              Peter Hansen <peter@engcorp. com> writes:
                              [color=blue]
                              > Hung Jung Lu wrote:[color=green]
                              > >
                              > > Peter Hansen <peter@engcorp. com> wrote in message news:<3F971996. 80B30C6D@engcor p.com>...[color=darkred]
                              > > > Anyone who believes there is or will be anything new in this discussion
                              > > > hasn't been reading comp.lang.pytho n or the mailing list nearly long enough.[/color]
                              > >
                              > > Yeah, the eternal, never-ending war between the tabibans and the tabifans.[/color]
                              >
                              > Careful. I think you've just attracted the attention of the Bush war machine...[/color]

                              We have always been at war with FourSpaceTabani a!

                              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

                              Comment

                              Working...