Postpone evaluation of argument
Paul Rubin
no.email at nospam.invalid
Fri Feb 10 18:57:56 EST 2012
Righard van Roy <pluijzer at gmail.com> 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
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