Python indentation

Sateesh ext-sateesh.kavuri at nokia.com
Thu Jul 8 02:09:45 EDT 2004


If you are used to coding in vi editor, it supports C (or for that matter
any language that uses braces) well. I feel the biggest advantage of using
braces is that I can know the start and end of the block of code, and this
becomes important if the code block is big and does not fit your screen. All
I need to do is to place the cursor on the { or } and press the key
combination shift+%. This will show the start and the end brace for that
block. And if there is an error, you will clearly know that using this key
combination. I have been using this method all the time, and it proved to be
really effective.
I am just wondering, how huge if, else blocks of code can be easily handled
in python. Is it not so anyone reading python code written by someone else
will get lost to find the start and end of these if, else blocks, or for
that matter any looping constructs.

Cheers!
Sateesh
"David Bolen" <db3l at fitlinxx.com> wrote in message
news:un02bz8b8.fsf at fitlinxx.com...
> Steve Lamb <grey at despair.dmiyu.org> writes:
>
> >     8 lines.  But that's because I never understood why anyone would
> > willingly use the '} else {' construct since the else is ovten
> > obscured.  Keyword opens the block, } ends the block, never put a
> > block open and close on the same line.
>
> Oh, I thought I wouldn't contribute to this thread, but what the hey...
>
> I certainly willingly use the "} else {" construct - probably to me
> because it stands out quite well and doesn't add additional noise
> along the left edge (the } and else on separate lines feels jarring to
> my eyes).  There's plenty of density in the "} else {" (as opposed to
> a simple closing brace) to draw my eye.
>
> Clearly it's an "in the eye of the beholder" thing.  I will admit to
> being a K&R "one true brace style" guy.  Maybe from having learned C
> with K&R, but possibly a subconcious effort to minimize my brace
> impact (minimize wasted empty brace only lines and impact on the
> visual blocks I was creating) on what was otherwise an indentation
> based block format I was trying to express.  Little did I know I
> really just wanted Python :-)
>
> Interestingly, I never even really considered the else and next
> opening brace ("else {") on its own line as an option - the folks that
> I've seen use else starting on its own line also tend to be those that
> prefer the Allman/BSD style of braces, in which case else would be
> alone on the line with the closing and opening braces on their own
> lines above and below it.  I don't mind BSD style, but it does eat up
> a lot of extra lines that only have braces on them, which cuts down on
> the density of code you can view easily on a single screen.
>
> Then again, I also see and feel similarity between my formatting of C
> in:
>
>     if (stuff) {
>         do something
>     } else {
>         do something else
>     }
>
> and my Python:
>
>     if stuff:
>         do something
>     else:
>         do something else
>
> is in terms of indentation and line position of portions of the flow
> control.  The only brace in the C code that unnecessarily adds lines
> is the final closing one.  Of course I'm sure the same argument could
> be worded about the other popular formats.  To each his own :-)
>
> -- David





More information about the Python-list mailing list