Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Postpone evaluation of argument

Reply
Thread Tools

Postpone evaluation of argument

 
 
Righard van Roy
Guest
Posts: n/a
 
      02-10-2012
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.

Thank you,
Righard
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      02-10-2012
Righard van Roy <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Jussi Piitulainen
Guest
Posts: n/a
 
      02-11-2012
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.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      02-11-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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DOCTYPE: How to postpone DTD location resolution at runtime Jesse XML 4 12-13-2007 04:43 PM
Postpone creation of attributes until needed Frank Millman Python 23 06-13-2007 02:42 PM
Pioneer to Postpone Blu-ray in Europe wandrinandz DVD Video 0 06-20-2006 12:27 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
py-xmlrpc postpone question elbertlev@hotmail.com Python 0 01-31-2005 08:03 PM



Advertisments