On Thu, Jul 07, 2005 at 03:03:35PM -0400, Phillip J. Eby wrote:
> At 02:48 PM 7/7/2005 -0400, Tim Peters wrote:
> >[Guido, on {for,while}/else]
> >...
> > > The question remains whether Python would be easier to learn without
> > > them. And if so, the question would remain whether that's offset by
> > > their utility for experienced developers. All hard to assess
> > > impartially!
> >
> >That's what I'm here for. I like loop "else" clauses, but have to
> >admit that (a) I rarely use them; (b) most of the time I use them, my
> >control flow is on the way to becoming so convoluted that I'm going to
> >rewrite the whole function soon anyway;
>
> Interesting; I usually intentionally write "else" clauses intending to
> *clarify* the code. That is, I often use it in cases where it's not
> strictly necessary, e.g.:
>
> for thing in something:
> if blah:
> return thing
> else:
> return None
>
> Because to me this clarifies that 'return None' is what happens if the loop
> "fails".
I use else similarly, for defensive programming.
for (thing) in searchlist:
if (matches(thing)):
keeper = thing
break
else:
raise ValueError("No thing matches()")
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.
-Jack
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com