Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49778
| References | <fdc5f317fd454a4dbb9b3033dbe18a60@exch.activenetwerx.com> |
|---|---|
| From | Joshua Landau <joshua.landau.ws@gmail.com> |
| Date | 2013-07-03 23:19 +0100 |
| Subject | Re: Decorator help |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4185.1372889990.3114.python-list@python.org> (permalink) |
On 3 July 2013 23:09, Joseph L. Casale <jcasale@activenetwerx.com> wrote:
> I have a set of methods which take args that I decorate twice,
>
> def wrapped(func):
> def wrap(*args, **kwargs):
> try:
> val = func(*args, **kwargs)
> # some work
> except BaseException as error:
> log.exception(error)
> return []
> return wrap
>
> def wrapped_again(length):
> def something(func):
> def wrapped_func(*args, **kwargs):
> values = func(*args, **kwargs)
> # do some work
> return values
> return wrapped_func
> return something
>
> So the methods wrapped are as follows:
>
> @wrapped_again(12)
> @wrapped
> def class_method(self, **kwargs):
> #....
>
> Is it possible to get the name of the original method (class_method) from within wrapped_func inside wrapped_again?
> Thanks!
Normally you'd want to use functools.wraps;
def wrapped(func):
@functools.wraps
def wrap(*args, **kwargs): ...
return wrap
def wrapped_again(length):
@functools.wraps
def something(func): ...
return something
@wrapped_again(12)
@wrapped
def class_method(self, **kwargs):
....
And then the name is "carried", as with docstrings.
If you don't want to do that, you'd need to use introspection of a
remarkably hacky sort. If you want that, well, it'll take a mo.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Decorator help Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-03 23:19 +0100
csiph-web