Postpone evaluation of argument

88888 Dihedral dihedral88888 at googlemail.com
Sat Feb 11 12:16:39 CET 2012


在 2012年2月11日星期六UTC+8上午7时57分56秒,Paul Rubin写道:
> Righard van Roy 
>  writes:
> > I want to add an item to a list, except if the evaluation of that item
> > results in an exception.
> 
> This may be overkill and probably slow, but perhaps most in the spirit
> that you're asking.
> 
>     from itertools import chain
> 
>     def r(x):
>         if x > 3:
>             raise(ValueError)
>         return x
> 
>     def maybe(func):
>       try:
>          yield func()
>       except:
>          return
> 
I am wondering at which level to yield in a nested decorated function
is more controllable. 

It is definitely wrong to yield in manny levels decorated.


>     def p(i): return maybe(lambda: r(i))
> 
>     your_list = list(chain(p(1), p(5)))
>     print your_list




More information about the Python-list mailing list