Python indentation (3 spaces)

Peter J. Holzer hjp-python at hjp.at
Mon Oct 15 15:28:58 EDT 2018


On 2018-10-15 12:06:06 +0100, Rhodri James wrote:
> On 14/10/18 09:06, Peter J. Holzer wrote:
> > If everybody used tabs, why would anyone care about your tab width?
> > Unless they look over your shoulder while you are typing, they won't
> > even know how wide your tabs are.
> 
> If you used two-space tab stops and I used (the normal :-) eight,
> comfortable indentations for you would rapidly become uncomfortably large
> indentations for me.  I'd care about that.

Buy a bigger monitor :-).

On a more serious note, I think the number of indentation levels should
be fairly low regardless of their width. If someone uses 2-space
indentation so they can fit 20 nested ifs, the other programmers should
complain during code review: Not because the lines are too long but
because the nesting is too deep.

> I'd also care if you used spaces to make sub-tab stop adjustments to
> line up appropriate text (inside brackets, for example).

I'm not sure what a "sub-tab stop adjustment" is. I think that for the
"1 tab per indentation level" style, tabs would be used only for
indentation, nothing else, and preferably only for block-level
indentation.

So a python function might look like this with 8 spaces per tab:

def·foo(a_parameter,·another_parameter,
········an_optional_parameter=DEFAULT_VALUE,
········another_optional_parameter=None
····):
       →x·=·a_parameter
       →a·=·[
       →····{·'x':·1,···'y':·2,····'z':·3·},
       →····{·'x':·0.4,·'y':·3.14,·'z':·1.618·},
       →]
       →if·another_optional_parameter:
       →       →r·=·(
       →       →····(another_optional_parameter·*·a[0]['x']·+·another_parameter·*·b[0]['y'])
       →       →····/
       →       →····(a[1]['x']·+·a[1]['y']·+·a[1]['z'])
       →       →)
       →else:
       →       →r·=·another_parameter·*·0
       →return·r

(I'using     → to represent a tab here and · to represent a space.)

And like this with 2 spaces per tab:

def·foo(a_parameter,·another_parameter,
········an_optional_parameter=DEFAULT_VALUE,
········another_optional_parameter=None
····):
 →x·=·a_parameter
 →a·=·[
 →····{·'x':·1,···'y':·2,····'z':·3·},
 →····{·'x':·0.4,·'y':·3.14,·'z':·1.618·},
 →]
 →if·another_optional_parameter:
 → →r·=·(
 → →····(another_optional_parameter·*·a[0]['x']·+·another_parameter·*·b[0]['y'])
 → →····/
 → →····(a[1]['x']·+·a[1]['y']·+·a[1]['z'])
 → →)
 →else:
 → →r·=·another_parameter·*·0
 →return·r

Note that everything still lines up nicely.

	hp (a four spaces per indentation level guy)

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20181015/2c99470c/attachment.sig>


More information about the Python-list mailing list