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


Groups > comp.lang.python > #89441

Re: Function decorator having arguments is complicated

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'explicitly': 0.05; 'arguments': 0.09; 'decorator': 0.09; 'subject:Function': 0.09; 'variables.': 0.09; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'class:': 0.16; 'complicated,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'levels,': 0.16; 'nesting': 0.16; 'referencing': 0.16; 'repetition': 0.16; 'scope,': 0.16; 'wrote:': 0.18; 'to:name:python-list@python.org': 0.22; 'mon,': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'class': 0.32; 'skip:_ 10': 0.34; 'received:google.com': 0.35; 'really': 0.36; 'method': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'simple': 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'gain': 0.79; '2015': 0.84; 'ethan': 0.84; 'furman': 0.84; 'n):': 0.84; 'self.n': 0.84; 'capture': 0.91
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:to :content-type; bh=myhBfyqI6i10WIti7aXfPsnLc8flHLJ1V6NM7W9LIAQ=; b=ynwfhtTKxUKlRKjyKPA9F9ALR1cJH/vCmK91AJ0+dhomUOglMNf36EoG44kW2JVVMm L6HQV+fm4LFdrOGoJX7YvWSgjUodR0MUyrTUnKV6sTAk6awgyZwJy5VrcPf+ahJMU8a1 qb1Xymwgihh35xrvkhAdCQ/me2D1LDOiC/p4Yeed7myy7P49Ji5sXODDo89BiQo4/qOy EYKx8jVv+ptg3r8+143OofoDdNTvRxrl6qty5a0LEw9MJCFe0w98iN7RtQFW4z+FVlfM 0iOKSoiUPBXOjS8n4+YSOkjbk7Tg/0WFss+yxFB3m9RT+luPyeMsm9Z5JlsMsS3X+2hu EAqA==
MIME-Version 1.0
X-Received by 10.50.176.137 with SMTP id ci9mr11169891igc.2.1430109125414; Sun, 26 Apr 2015 21:32:05 -0700 (PDT)
In-Reply-To <20150427042426.GD32422@stoneleaf.us>
References <CAFTm5Ru5NbFOtLPoVSPujeakrGawdHXjNkWJOVs1XrvBNnOUOQ@mail.gmail.com> <20150427042426.GD32422@stoneleaf.us>
Date Mon, 27 Apr 2015 14:32:05 +1000
Subject Re: Function decorator having arguments is complicated
From Chris Angelico <rosuav@gmail.com>
To "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.20+
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.35.1430109133.3680.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430109133 news.xs4all.nl 2920 [2001:888:2000:d::a6]:44740
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89441

Show key headers only | View raw


On Mon, Apr 27, 2015 at 2:24 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
> On 04/27, Makoto Kuwata wrote:
>>
>> I feel that function decorator having arguments is complicated,
>> because three 'def' are nested:
>>
>>   def multiply(n):
>>     def deco(func):
>>       def newfunc(*args, **kwargs):
>>         return n * func(*args, **kwargs)
>>       return newfunc
>>     return deco
>
> When I have to write an argument-taking decorator, I use a class:
>
>     class multiply(object):  # don't need 'object in 3.x'
>
>         def __init__(self, n):
>             self.n = n
>
>         def __call__(self, func):
>             def newfunc(*args, **kwargs):
>                 return self.n * func(*args, **kwargs)
>             return newfunc

What's the advantage of that over a simple closure? You have the same
number of nesting levels, plus a lot more boiler-plate repetition -
instead of just referencing names from the outer scope, you have to
explicitly capture them all with "self.n=n" for each one. I'm not sure
you really even gain much clarity.

In a way, a closure is a short-hand for an object with a __call__
method that auto-captures all its local variables.

ChrisA

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


Thread

Re: Function decorator having arguments is complicated Chris Angelico <rosuav@gmail.com> - 2015-04-27 14:32 +1000

csiph-web