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

Discussion in 'Python' started by Ian Bicking, Oct 22, 2003.

  1. Ian Bicking

    Ian Bicking Guest

    On Wednesday, October 22, 2003, at 05:31 PM, 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!!!!


    You can, it's just kind of annoying. Typically I work with two indents
    at a minimum, because most (of my) code is in a method of a class.
    With 8 spaces that's 20% of the space gone. And when using long names,
    that means I can end up with word wrapping problems pretty quickly.
    Even if I do as little nesting as possible, consider this (quite
    reasonable) level of nesting:

    class Whatever:
    def method(self, someArg, someOtherArg):
    for smallerPiece in someArg:
    if smallerPiece.isValid():
    newSmallerPiece =
    someOtherArg.doSomethingWith(smallerPiece)

    I hope that turns out okay, since my mail client wrapped it (but then
    that was the point ;). I would not consider this level of nesting to
    be bad programming, or programming that is in need of refactoring. I
    didn't even do tuple unpacking in that assignment...

    Maybe I would be more concerned with too much nesting if I was
    programming in C. C is more apt to have subtle and dangerous problems,
    so you want to avoid even localized complexity. You do a lot more
    things in place in C, while Python uses more return values. Because of
    exceptions you don't have to use the small chunks of code that C
    requires for error detection.

    But that's just my own opinion, you can do what you want (just don't
    use tabs ;).

    --
    Ian Bicking | | http://blog.ianbicking.org
     
    Ian Bicking, Oct 22, 2003
    #1
    1. Advertising

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

    Ian

    I thought about your example and adjusted last line to fit in 80 columns...

    class Whatever:
    def method(self, someArg, someOtherArg):
    for smallerPiece in someArg:
    if smallerPiece.isValid():
    newSmallerPiece =
    someOtherArg.doSomethingWith(smallerPiece)


    Here is the 4 space indent version:

    class Whatever:
    def method(self, someArg, someOtherArg):
    for smallerPiece in someArg:
    if smallerPiece.isValid():
    newSmallerPiece = someOtherArg.doSomethingWith(smallerPiece)


    Each has trade offs.... I don't like breaking last line into 2 pieces
    but at least "class", "def", "for" and "if" don't blend into each
    other as easily as in the 2nd example.

    I'm not convinced yet of 4 space tabs but your point is well taken.

    Thanks,

    Chris





    Ian Bicking <> wrote in message news:<>...
    > On Wednesday, October 22, 2003, at 05:31 PM, 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!!!!

    >
    > You can, it's just kind of annoying. Typically I work with two indents
    > at a minimum, because most (of my) code is in a method of a class.
    > With 8 spaces that's 20% of the space gone. And when using long names,
    > that means I can end up with word wrapping problems pretty quickly.
    > Even if I do as little nesting as possible, consider this (quite
    > reasonable) level of nesting:
    >
    > class Whatever:
    > def method(self, someArg, someOtherArg):
    > for smallerPiece in someArg:
    > if smallerPiece.isValid():
    > newSmallerPiece =
    > someOtherArg.doSomethingWith(smallerPiece)
    >
    > I hope that turns out okay, since my mail client wrapped it (but then
    > that was the point ;). I would not consider this level of nesting to
    > be bad programming, or programming that is in need of refactoring. I
    > didn't even do tuple unpacking in that assignment...
    >
    > Maybe I would be more concerned with too much nesting if I was
    > programming in C. C is more apt to have subtle and dangerous problems,
    > so you want to avoid even localized complexity. You do a lot more
    > things in place in C, while Python uses more return values. Because of
    > exceptions you don't have to use the small chunks of code that C
    > requires for error detection.
    >
    > But that's just my own opinion, you can do what you want (just don't
    > use tabs ;).
     
    Christian Seberino, Oct 23, 2003
    #2
    1. Advertising

  3. Ian Bicking

    Steve Lamb Guest

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

    On 2003-10-23, Christian Seberino <> wrote:
    > Each has trade offs.... I don't like breaking last line into 2 pieces
    > but at least "class", "def", "for" and "if" don't blend into each
    > other as easily as in the 2nd example.


    They blend in? How so? 8 spaces is way too far for the eyes, or at least
    *my* eyes, to skip without causing serious pauses. 2 spaces, while I use it
    immensely in my Perl days, is where blending occurs. 4 spaces it's pretty
    clear that the next line down is a separate block while not having to jump
    partway to Siberia to indicate it. Furthremore at 4 spaces you have a
    continuation of the lines above to the lines below in even the shortest of
    lines.

    if x:
    x = y
    elsif y:
    y = z
    else z:
    z = x

    vs.

    if x:
    x = y
    elsif y:
    y = z
    else:
    z = x

    Notice that on the 2nd example the block isn't under the control
    statements at all. There's no relation of those blocks to the statements
    above it. To me that is the biggest thing. There's separation but not so
    much that there isn't flow.

    if x:
    1234v

    else:
    1234v

    Of course, this is all personal preference. You want 8, have fun. :p

    --
    Steve C. Lamb | I'm your priest, I'm your shrink, I'm your
    PGP Key: 8B6E99C5 | main connection to the switchboard of souls.
    -------------------------------+---------------------------------------------
     
    Steve Lamb, Oct 23, 2003
    #3
    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:
    431
    Soren Kuula
    May 27, 2005
  2. Christian Seberino
    Replies:
    21
    Views:
    1,719
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    751
    Michael Hudson
    Oct 24, 2003
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,076
    Smokey Grindel
    Dec 2, 2006
  5. Jason
    Replies:
    0
    Views:
    215
    Jason
    Jul 6, 2004
Loading...

Share This Page