newbie: stani's python editor if-else

Steve Holden steve at
Tue Sep 11 14:32:37 CEST 2007

madzientist wrote:
> hi,
> two quick questions:
> a) i am using SPE (latest version) and for some reason, when i type,
> say
> if 1==2:
>     print "not equal"
>     else:
>           print "equal"
> the else is at the same indentation level as the preceding print
> statement, and i get a syntax error
> why doesn't spe automatically put the else at the level of the if
> statement ? what am i dong wrong ? once i manually change the
> indentation, the code snippet runs perfectly.
> b) if this is not the group for such elementary questions, please do
> let me know.
> thanks !!!!
> suresh

The point here is that SPE isn't analyzing your source in sufficient 
detail to recognize the need to "outdent" the else statement. Clearly 
there could be any number of statements at the same indent level as the 
first print statement.

For the record this is not a behavior restricted to SPE, but also occurs 
in Wing IDE, PythonWin and Scite, for starters.

Technically you are correct in the example you give: an IDE could decide 
to remove one level of indentation, but of course there's always the 
"dangling else" problem: if one "if" statement is nested inside another 
one, which indentation level would be appropriate for an "else"?

Generally speaking, only the programmer can know, and so generally you 
are left to enforce these issues for yourself.

Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd 
Skype: holdenweb
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

More information about the Python-list mailing list