Re: Postpone evaluation of argument
- From: Paul Rubin <no.email@xxxxxxxxxxxxxx>
- Date: Fri, 10 Feb 2012 15:57:56 -0800
Righard van Roy <pluijzer@xxxxxxxxx> 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
.
- References:
- Postpone evaluation of argument
- From: Righard van Roy
- Postpone evaluation of argument
- Prev by Date: Re: How can I catch misnamed variables?
- Next by Date: Re: Fabric Engine + Python benchmarks
- Previous by thread: Postpone evaluation of argument
- Next by thread: Re: Postpone evaluation of argument
- Index(es):
Relevant Pages
|