Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7455
| Date | 2011-06-11 21:27 +0200 |
|---|---|
| Subject | How to avoid "()" when writing a decorator accepting optional arguments? |
| From | Giampaolo RodolĂ <g.rodola@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.133.1307820426.11593.python-list@python.org> (permalink) |
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/
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
How to avoid "()" when writing a decorator accepting optional arguments? Giampaolo RodolĂ <g.rodola@gmail.com> - 2011-06-11 21:27 +0200 Re: How to avoid "()" when writing a decorator accepting optional arguments? zeekay <zachkelling@gmail.com> - 2011-06-16 04:10 -0700
csiph-web