By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

Colons, indentation and reformatting. (2)

P: n/a
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/genera...ns-required-fo...
Jan 9 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Paddy wrote:
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/genera...ns-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.
How would you re-indent this?

if x>0: print x

>
Tell me it is not so, or I will be editing the Wikipedia page I think.

- Paddy.
Jan 9 '07 #2

P: n/a
"Paddy" <pa*******@netscape.netwrote in message
news:11**********************@i15g2000cwa.googlegr oups.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/genera...ns-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
Jan 9 '07 #3

P: n/a

me********@aol.com wrote:
Paddy wrote:
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/genera...ns-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.

How would you re-indent this?

if x>0: print x
If pasted as a line , after a line *ending with* a colon then indent it
w.r.t. previous line.
if pasting full lines after such a line then first pasted line cannot
be indented more than this line; if it is then flag for re-indenting
pasted block either equal too or less than this line.

- Paddy.

Jan 9 '07 #4

P: n/a

Paul McGuire wrote:
"Paddy" <pa*******@netscape.netwrote in message
news:11**********************@i15g2000cwa.googlegr oups.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/genera...ns-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.

Jan 9 '07 #5

P: n/a
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.

Could be done that way - but is it a killer feature? I doubt it, but
maybe you convince somebody to implement it.

Yet it certainly doesn't justify a wikipedia edit - the FAQ entry is
right, and there is no way of auto-indenting python code that for some
reason suffered from indention mix-up.

Diez
Jan 9 '07 #6

P: n/a

OK, whilst colons are not sufficient to re-format a completely
mis-indented file. I'm thinking that they are sufficient for
reformatting most pasted code blocks when refactoring say?
- Paddy.

Jan 9 '07 #7

P: n/a
"Paddy" <pa*******@netscape.netwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
>
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.
How would the user know this? Every dedent is ambiguous, since there is no
punctuation to indicate it.

-- Paul
Jan 9 '07 #8

P: n/a
On 8 Jan 2007 23:57:29 -0800, Paddy <pa*******@netscape.netwrote:
>
OK, whilst colons are not sufficient to re-format a completely
mis-indented file. I'm thinking that they are sufficient for
reformatting most pasted code blocks when refactoring say?
Let's put it this way: if the formatter can assume the original code is
valid (i.e. has the intended indentation) then it can do all kinds of nifty
things to it.

Personally, I'm happy with what Emacs' python-mode offers: suggested indents
as I type, and a command to indent/de-dent the highlighted block one step.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jan 9 '07 #9

P: n/a

"Jorgen Grahn" <gr********@snipabacken.dyndns.org>wrote:
On 8 Jan 2007 23:57:29 -0800, Paddy <pa*******@netscape.netwrote:

OK, whilst colons are not sufficient to re-format a completely
mis-indented file. I'm thinking that they are sufficient for
reformatting most pasted code blocks when refactoring say?

Let's put it this way: if the formatter can assume the original code is
valid (i.e. has the intended indentation) then it can do all kinds of nifty
things to it.
This is true - and I think it will only fail if the "entry" point in the pasted
code is "further to the right" than where it has to fit in to the original
code - i.e. if you "run out of space" to the left. - but in that case
you really are hacking, and you are in urgent need of some slashing...

- Hendrik

Jan 10 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.