Colons, indentation and reformatting. (2)

P

Paddy

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.
 
M

mensanator

Paddy said:
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.

How would you re-indent this?

if x>0: print x
 
P

Paul McGuire

Paddy said:
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
 
P

Paddy

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.
 
P

Paddy

Paul said:
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.
 
D

Diez B. Roggisch

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
 
P

Paddy

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.
 
P

Paul McGuire

Paddy said:
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
 
J

Jorgen Grahn

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
 
H

Hendrik van Rooyen

Jorgen Grahn said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top