[Python-ideas] Fwd: quantifications, and tuple patterns

Simon Sapin simon.sapin at kozea.fr
Sun Jan 15 11:54:20 CET 2012


Le 15/01/2012 03:01, Steven D'Aprano a écrit :
> def any2(iterable, pred=bool):
> """Like any(pred(x) for x in iterable), returning False if it is false,
> otherwise obj, the first witness that it is true.
> """
> for obj in iterable:
> if pred(obj):
> return obj
> # I look forward to the bike-shedding about returning None
> # vs returning False;)
> return False

Hi,
If the point is to get some value that matches a predicate, how about this?
next(value for value in iterable if predicate(value))
If a "no value" marker is desired instead of StopIteration:
next((value for value in iterable if predicate(value)), None)
But yes, assignment-expression is still required to avoid the while 
True/if/break dance.
Regards,
-- 
Simon Sapin


More information about the Python-ideas mailing list

AltStyle によって変換されたページ (->オリジナル) /