Postpone evaluation of argument

Paul Rubin at nospam.invalid
Fri Feb 10 18:57:56 EST 2012

Righard van Roy <pluijzer at> 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:
        return x

    def maybe(func):
         yield func()

    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