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

Christian Seberino seberino at spawar.navy.mil
Thu Oct 23 02:51:58 EDT 2003


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 <ianb at colorstudy.com> wrote in message news:<mailman.37.1066863551.702.python-list at python.org>...
> 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 ;).




More information about the Python-list mailing list