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


Groups > comp.lang.python > #77907

Re: functools.wraps behaviour

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; '16,': 0.03; 'argument': 0.05; 'output': 0.05; '(python': 0.07; '*args,': 0.09; 'arguments,': 0.09; 'subject:skip:f 10': 0.09; 'try:': 0.09; 'wrapped': 0.09; 'wrapper': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '**kwargs):': 0.16; '9:15': 0.16; 'args,': 0.16; 'args.': 0.16; 'consciously': 0.16; 'decision,': 0.16; 'exception:': 0.16; 'expected,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'positional': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'cc:addr:python.org': 0.22; 'error': 0.23; 'decide': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'change,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'sep': 0.31; "i'd": 0.34; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'reports': 0.37; 'two': 0.37; 'either': 0.39; 'skip:p 20': 0.39; "you're": 0.61; 'first': 0.61; 'name': 0.63; 'wrapper,': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=oJHn+jtNvYQlO+BncazlvdABpLtRW7/cbQq8NRIR7fg=; b=InnLFTKmKR9jrjvfjHSETuVPKbKROgNUSdzDOy7X3J3FJvTPB/lYdDNwKX2tLTbnLY m6jfbfb1j9acxRd3FfGUV+iimGrWEHMZmoreCEKBhVYblCj5EuCoJqr2L6cuUPf1xXLs gtS5py1BFwHgERkUgucMbOACh5yMbyoJHJIVW9JLpUfWCZQ6mmFqT/wlcyXoAaS1Msrl qM++uSCpXGwuva/Dq7zOcJTJxTwVHQWZDHP9SEgfvG+c799Iq3wfOC7y6zZNEVjTm/YV iBA4FgPxoaxmnxIZ3bn1WqtiRViqvNsAI5fk1q1lw1mrTcnuFLXRQQUwK8Soe4ssBu9G 8+Yw==
MIME-Version 1.0
X-Received by 10.112.167.195 with SMTP id zq3mr6841478lbb.90.1410828407699; Mon, 15 Sep 2014 17:46:47 -0700 (PDT)
In-Reply-To <541772fa$0$2078$426a74cc@news.free.fr>
References <541772fa$0$2078$426a74cc@news.free.fr>
Date Tue, 16 Sep 2014 10:46:47 +1000
Subject Re: functools.wraps behaviour
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14037.1410828831.18130.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1410828831 news.xs4all.nl 2876 [2001:888:2000:d::a6]:46222
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77907

Show key headers only | View raw


On Tue, Sep 16, 2014 at 9:15 AM, ISE Development <isenntp@gmail.com> wrote:
>         @functools.wraps(func)
>         def wrapper(self):
>             func(self)
>         return wrapper
>
>     try:
>         k.method(1)
>     except Exception as e:
>         print('exception:',e)
>
> The output (Python 3.3) is:
>
> Klass.method: <function Klass.method at 0x7f2d7c454b00>
> k.method <bound method Klass.method of <__main__.Klass object at
> 0x7f2d7c4570d0>>
> exception: wrapper() takes 1 positional argument but 2 were given
>
> The first two lines are as expected, using the name of the decorated
> function. However, the exception uses the name of the decorating wrapper
> function.

In your wrapper, you're swallowing all arguments. That means you're
consciously rejecting them, and passing none on. If you want a wrapper
to let the wrapped function decide about arguments, try this:

    def decorator(func):
        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):
            func(self, args, kwargs)
        return wrapper

With that change, the error message reports that it's method() that's
rejecting the args.

So yes, I'd say this is a feature; you can either let the wrapped
function make the decision, or you can have the wrapping function deal
with args.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

functools.wraps behaviour ISE Development <isenntp@gmail.com> - 2014-09-16 01:15 +0200
  Re: functools.wraps behaviour Chris Angelico <rosuav@gmail.com> - 2014-09-16 10:46 +1000
    Re: functools.wraps behaviour ISE Development <isenntp@gmail.com> - 2014-09-16 09:24 +0200
  Re: functools.wraps behaviour Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-15 21:39 -0600

csiph-web