Colons, indentation and reformatting. (2)

Paul McGuire ptmcg at austin.rr._bogus_.com
Tue Jan 9 01:45:56 EST 2007


"Paddy" <paddy3118 at netscape.net> wrote in message 
news:1168323368.592642.314130 at i15g2000cwa.googlegroups.com...
>I was just perusing a Wikipedia entry on the "off side rule" at
> http://en.wikipedia.org/wiki/Off-side_rule .
> It says that the colon in Python is purely for readability, and cites
> our FAQ entry
> http://www.python.org/doc/faq/general.html#why-are-colons-required-fo...
> .
> However, near the top of the Alternatives section, it states that for C
> type, curly braces using languages:
>  "An advantage of this is that program code can be automatically
> reformatted and neatly indented without fear of the block structure
> changing".
>
> Thinking about it a little, it seems that a colon followed by
> non-indented code that has just been pasted in could also be used by a
> Python-aware editor as a flag to re-indent the pasted code.
>
> Tell me it is not so, or I will be editing the Wikipedia page I think.
>
> - Paddy.
>
No, the ambiguity comes in when you have a nested construct within another 
nested construct. Here is some (fake) code where all the indentation was 
lost after pasting through a badly-behaved newsreader (this is NOT real 
code, I know that it wont really run, I'm just trying to demonstrate the 
indentation issue):

while x:
a = 100
if b > 3:
a += 1
b += 1

Here are some valid indented versions:

while x:
    a = 100
if b > 3:
    a += 1
b += 1

while x:
    a = 100
    if b > 3:
        a += 1
        b += 1

while x:
    a = 100
    if b > 3:
        a += 1
    b += 1

while x:
    a = 100
    if b > 3:
        a += 1
b += 1

The colons alone are not sufficient to tell us which is correct.

-- Paul





More information about the Python-list mailing list