Colons, indentation and reformatting. (2)

Paddy paddy3118 at netscape.net
Tue Jan 9 02:29:56 EST 2007


Paul McGuire wrote:

> "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
Won't the following rules work when pasting complete Python statements
and complete lines, after other lines in an editor:

lets call the line after which the block is to be pasted the paste
line, and the original indent of the first line of the copied block to
be pasted the copy indent.

If the paste line ends in a colon then the copy indent must be greater
than the paste line indent, or the copy block should be re-indented on
pasting to make it so.
If the paste line does not end in a colon then the copy block indent
should be equal too or less than the paste line indent. If this is not
the case then the user should be asked wether to re-indent the copy
block to be equal to, or de-dented w.r.t. the paste line indent prior
to pasting.

- Paddy.




More information about the Python-list mailing list