Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20204

Re: Postpone evaluation of argument

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'passes': 0.05; 'postpone': 0.07; 'decorator': 0.09; 'exception.': 0.09; 'def': 0.13; 'argument': 0.15; '"quote"': 0.16; 'except:': 0.16; 'magic.': 0.16; 'roy': 0.16; '\xc2\xa0if': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'arguments': 0.18; 'cheers,': 0.20; 'thus': 0.21; 'cc:no real name:2**0': 0.21; 'header:In- Reply-To:1': 0.22; 'feb': 0.22; 'cc:2**0': 0.26; 'looks': 0.27; 'putting': 0.28; 'message-id:@mail.gmail.com': 0.29; 'explicitly': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'chris': 0.30; 'van': 0.30; 'does': 0.32; 'list': 0.32; 'there': 0.33; 'fri,': 0.34; 'rather': 0.34; 'skip:@ 10': 0.34; 'try:': 0.34; 'received:209.85.214': 0.36; 'list,': 0.36; 'hello,': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'could': 0.38; 'i.e.': 0.39; 'except': 0.39; 'received:209': 0.39; 'results': 0.64; 'skip:\xc2 10': 0.74; 'delaying': 0.84; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84; 'evaluation.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=j1mHvTPLfJee7aFE6k5Pv5/Rg9AlQEMh9eVUv4ZpgI4=; b=QKTy37W/r2oquSrYZ+om2bcyZPdwI4KwqpUOtUQG7L4cGrG7oIUyoscCRn0DneOc+C ajnP8bpEBbikNNIcGY7SM2otTd45t2fQR3XaqLZmRdxV4+obZcd7tabCF1BhaA5uri2G PFN57RJKREA1hYGapPwndcxynztQLqb/1bqlo=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <CAN9UcZVjzKRaVYqjTky0Egdr6_9bvV4WpE9C5oWtc4pqoitE3Q@mail.gmail.com>
References <CAN9UcZVjzKRaVYqjTky0Egdr6_9bvV4WpE9C5oWtc4pqoitE3Q@mail.gmail.com>
Date Fri, 10 Feb 2012 15:12:38 -0800
X-Google-Sender-Auth hM1CyW_1oo5P8KOBnbCtvEDa60I
Subject Re: Postpone evaluation of argument
From Chris Rebert <clp2@rebertia.com>
To Righard van Roy <pluijzer@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Gm-Message-State ALoCoQk8D/f+vD/zvqD2u2uSL9fLM/u3BpEaPARL8MxY0iC9dS1U5ABbHo3Uut4ertpz7fVFtu0D
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5687.1328915560.27778.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328915560 news.xs4all.nl 6971 [2001:888:2000:d::a6]:45316
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20204

Show key headers only | View raw


On Fri, Feb 10, 2012 at 3:01 PM, Righard van Roy <pluijzer@gmail.com> wrote:
> 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.

Nope. All arguments always get evaluated before control passes to the
callee. You'd have to "quote" the arguments manually by putting them
in lambdas, thus explicitly delaying their evaluation.

Cheers,
Chris
--
http://rebertia.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Postpone evaluation of argument Chris Rebert <clp2@rebertia.com> - 2012-02-10 15:12 -0800

csiph-web