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


Groups > comp.lang.python > #49778

Re: Decorator help

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.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; 'args': 0.07; 'error:': 0.07; 'subject:help': 0.08; 'try:': 0.09; 'val': 0.09; 'wrapped': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'wrote:': 0.18; 'normally': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:@ 10': 0.30; 'message- id:@mail.gmail.com': 0.30; 'thanks!': 0.32; 'except': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'possible': 0.36; 'follows:': 0.38; 'that,': 0.38; 'name': 0.63; 'july': 0.63; 'within': 0.65; 'decorate': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=O4gN7RWLAXi4iSOIVOP1ffd2W3gGPwlzO3WIOdc/F10=; b=v2jQkkxEwIU5Rjg9gVEu87XelbtjdosoznUXutE6NOqrQENoLH4w9+klYq11SIBOf0 DeYhozbrDKCfuZ+Q/GA1GA9IstNI5KbuuA7Xtf6v5TyJrvchRH3h/kRYVERjKI1OZF8e 7jc0Ryuet2fwCNAoWQej7Tya05OHLmusUEvSyr5oyZ+G3xOxSQMJT3ZTxd5tow18RGt2 AI6uVaKiZ2dxmxi9EDO+sXirXG48lzJkIGOK5YgXGWvua2oN3wkQdtFfvz3PwQhpe/Zh KsD9W8HUIkOuRbWQgj7ClRm93sJDgouTlCzb7Yu8ufzPBXNxg9r9OgeZ5MFKkPK6D8oU 8jVw==
X-Received by 10.112.5.199 with SMTP id u7mr2165370lbu.67.1372889982381; Wed, 03 Jul 2013 15:19:42 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <fdc5f317fd454a4dbb9b3033dbe18a60@exch.activenetwerx.com>
References <fdc5f317fd454a4dbb9b3033dbe18a60@exch.activenetwerx.com>
From Joshua Landau <joshua.landau.ws@gmail.com>
Date Wed, 3 Jul 2013 23:19:02 +0100
Subject Re: Decorator help
To "Joseph L. Casale" <jcasale@activenetwerx.com>
Content-Type text/plain; charset=UTF-8
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.4185.1372889990.3114.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372889990 news.xs4all.nl 15986 [2001:888:2000:d::a6]:47040
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49778

Show key headers only | View raw


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


Thread

Re: Decorator help Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-03 23:19 +0100

csiph-web