Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"""': 0.07; 'args': 0.07; 'subject:when': 0.07; 'argument:': 0.09; 'callable': 0.09; 'decorator': 0.09; 'none:': 0.09; 'omit': 0.09; 'def': 0.12; 'written': 0.14; '"""a': 0.16; '"%s': 0.16; '(optionally)': 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'deprecate': 0.16; 'deprecated.': 0.16; 'inner(*args,': 0.16; 'instead"': 0.16; 'parentheses:': 0.16; 'subject:writing': 0.16; 'url:code': 0.17; 'modify': 0.22; 'optional': 0.23; 'received:209.85.210.174': 0.23; 'received:mail-iy0-f174.google.com': 0.23; 'function': 0.25; '---': 0.28; 'message-id:@mail.gmail.com': 0.28; 'subject:?': 0.29; 'subject:How': 0.30; 'outer': 0.30; 'to:addr:python-list': 0.33; 'from:charset:iso-8859-1': 0.33; "i've": 0.33; 'function.': 0.35; 'replacement': 0.35; 'skip:@ 10': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'url:google': 0.38; 'called': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'url:p': 0.63 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=EOiwkbU9aBiTD+gvhk5SPG/PwhvnZb8laVUnxaIXlK4=; b=IfTDSHanbDxv6tICtmDWNtXWnxtrK58C32np3BrjvidmGr9zd9prJr7Unw2jG9/NjH jrr6aTlkxPv/7oyTu82hlNBIH5BZPQxh2zzGDZR4OK1qgIbwqHoxiJ2yFSlGRqAB0jJX TOLYvyKWqhPH82SrWb7cDWWKWSxQd6MU+c8Uc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=r0RICLnI22jQRwvqSX1CNTv0i22rmb/HiafByEWQi4j3IDaFNm8ypkzKqxF3T11qZi iif8dcFSY6QobhhSBL3lUemh87kW78q2cGwJGd4roHkP7MPq894qJmJXyhiS8Un7hrkb rPvNhpL57o72ngbZZRe47vTs4n772aO/yWvSc= MIME-Version: 1.0 Date: Sat, 11 Jun 2011 21:27:03 +0200 Subject: How to avoid "()" when writing a decorator accepting optional arguments? From: =?ISO-8859-1?Q?Giampaolo_Rodol=E0?= 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: 46 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307820426 news.xs4all.nl 49184 [::ffff:82.94.164.166]:43526 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7455 I've written this decorator to deprecate a function and (optionally) provide a callable as replacement def deprecated(repfun=None): """A decorator which can be used to mark functions as deprecated. Optional repfun is a callable that will be called with the same args as the decorated function. """ def outer(fun): def inner(*args, **kwargs): msg = "%s is deprecated" % fun.__name__ if repfun is not None: msg += "; use %s instead" % (repfun.__name__) warnings.warn(msg, category=DeprecationWarning, stacklevel=2) if repfun is not None: return repfun(*args, **kwargs) else: return fun(*args, **kwargs) return inner return outer Now, I can use my decorator as such: @deprecated() def foo(): return 0 ...or provide an optional argument: @deprecated(some_function) def foo(): return 0 ...but I don't know how to modify it so that I can omit parentheses: @deprecated def foo(): return 0 Any hint? --- Giampaolo http://code.google.com/p/pyftpdlib/ http://code.google.com/p/psutil/