Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'postpone': 0.07; 'decorator': 0.09; 'exception.': 0.09; 'def': 0.13; 'argument': 0.15; 'except:': 0.16; 'magic.': 0.16; 'this:': 0.16; 'looks': 0.27; 'pass': 0.29; 'message-id:@mail.gmail.com': 0.29; 'does': 0.32; 'list': 0.32; 'there': 0.33; 'rather': 0.34; 'skip:@ 10': 0.34; 'try:': 0.34; 'thank': 0.35; 'to:addr:python-list': 0.35; '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; 'to:addr:python.org': 0.40; 'results': 0.64 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=coAUf0fmOGT459GEm2NyeVp0H3OfbyL9kF3hiJ3wy6o=; b=xlI/lREXSCORbr0/pcB+1DE2R7DRPsfIu4xkIPzjdZydSxfK2lPau/yWsKC3A52oZc 1RqMJc3K7Ch3r20aDJ14sEFtS6BPpQQQoxzlcDXX9EDhhEYSGxNkkpBNnMYE12dZ+abY /9okolPHP4LsLZ9sHzvEBbTJDEEf/UqYQef9k= MIME-Version: 1.0 Date: Sat, 11 Feb 2012 00:01:52 +0100 Subject: Postpone evaluation of argument From: Righard van Roy To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328914914 news.xs4all.nl 6934 [2001:888:2000:d::a6]:34245 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20202 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