newbie: stani's python editor if-else

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Sep 11 16:46:10 CEST 2007


Steve Holden a écrit :
> 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 ? 
(snip)
> 
> 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.

Emacs does a good job here - FWIW, it solves the "dangling else" problem 
by deindenting one level, then it's up to you to deindent more if 
appropriate. In practice, it turns out to be right most of the times, 
and not a problem in the few special cases.



More information about the Python-list mailing list