Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20217
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Postpone evaluation of argument |
| Date | 2012-02-11 10:54 +0200 |
| Organization | University of Helsinki |
| Message-ID | <qotk43tyclp.fsf@ruuvi.it.helsinki.fi> (permalink) |
| References | <mailman.5686.1328914914.27778.python-list@python.org> |
Righard van Roy writes:
> Hello,
>
> I want to add an item to a list, except if the evaluation of that item
> results in an exception.
> I could do that like this:
>
> def r(x):
> if x > 3:
> raise(ValueError)
>
> try:
> list.append(r(1))
> except:
> pass
> try:
> list.append(r(5))
> except:
> pass
>
> This looks rather clumbsy though, and it does not work with i.e. list
> comprehensions.
>
> I was thinking of writing a decorator like this:
>
> def tryAppendDecorator(fn):
> def new(*args):
> try:
> fn(*args)
> except:
> pass
> return new
>
> @tryAppendDecorator
> def tryAppend(list, item):
> list.append(item)
>
> tryAppend(list, r(1))
> tryAppend(list, r(5))
>
> This does not work however because the 'item' argument gets evaluated
> before the decorator does it's magic.
>
> Is there a way to postpone the evaluation of 'item' till it gets used
> inside the decorator. Like it is possible to quote a form in Lisp.
That's not considered good practice in Lisp either. One would use a
lambda expression to delay the computation, as others have suggested.
You might be able to arrange your program so that tryAppend is called
with the error-raising function and its arguments separately. I mean
like this:
def r(x):
if x > 3:
raise(ValueError)
return x
def tryAppendDecorator(fn):
def new(xs, f, *args):
try:
fn(xs, f(*args))
except:
pass
return new
@tryAppendDecorator
def tryAppend(items, item):
items.append(item)
sub3 = []
tryAppend(sub3, r, 3)
tryAppend(sub3, r, 1)
tryAppend(sub3, r, 4)
tryAppend(sub3, r, 1)
tryAppend(sub3, r, 5)
Maybe you should only ignore some specific type of exception, like
ValueError if you are specifically using r as a filter whose task it
is to raise a ValueError.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Postpone evaluation of argument Righard van Roy <pluijzer@gmail.com> - 2012-02-11 00:01 +0100
Re: Postpone evaluation of argument Paul Rubin <no.email@nospam.invalid> - 2012-02-10 15:57 -0800
Re: Postpone evaluation of argument 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-11 03:16 -0800
Re: Postpone evaluation of argument Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-02-11 10:54 +0200
csiph-web