Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"else" considered harmful in Python?

In an answer (by S.Lott) to a question about Python's try...else statement:

Actually, even on an if-statement, the else: can be abused in truly terrible ways creating bugs that are very hard to find. [...]

Think twice about else:. It is generally a problem. Avoid it except in an if-statement and even then consider documenting the else- condition to make it explicit.

Is this a widely held opinion? Is else considered harmful?

Of course you can write confusing code with it but that's true of any other language construct. Even Python's for...else seems to me a very handy thing to have (less so for try...else).

like image 751
dF. Avatar asked May 14 '09 21:05

dF.


1 Answers

S.Lott has obviously seen some bad code out there. Haven't we all? I do not consider else harmful, though I've seen it used to write bad code. In those cases, all the surrounding code has been bad as well, so why blame poor else?

like image 175
dwc Avatar answered Oct 08 '22 01:10

dwc