[Python-Dev] Chaining try statements: eltry?

Thomas Lotze thomas at thomas-lotze.de
Fri Jul 8 02:51:28 CEST 2005


Jack Diederich <jack at performancedrivers.com> wrote:

> I can't say I use it for much else, if I really want a default I do
> found = None
> for (thing) in searchlist:
>   if (matches(thing)):
>     found = None
>     break
> 
> That could end with 'else: found = None' to assign a default but I
> like the default to come first for readability.

Actually I think assigning the fall-back value in an "else" branch is
more readable. To some extent it's just a feeling, but there's two
things: For one, it gives you all assignments to the result at closer,
if not the same, indentation levels. More importantly, the value is
unconditionally set by a single statement, namely the "for" construct,
instead of by either the first or both, the second assignment basically
making the first superfluous. Using "else" models better what you want
to do, IMO.

-- 

Viele Grüße,
Thomas


More information about the Python-Dev mailing list