Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'explicitly': 0.05; 'arguments': 0.09; 'decorator': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'subject:Function': 0.09; 'variables.': 0.09; '~ethan~': 0.09; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'both,': 0.16; 'class:': 0.16; 'complicated,': 0.16; 'levels,': 0.16; 'nesting': 0.16; 'referencing': 0.16; 'repetition': 0.16; 'scope,': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'mon,': 0.24; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'on,': 0.29; 'especially': 0.30; "i'm": 0.30; 'easier': 0.31; 'class': 0.32; 'cases': 0.33; 'skip:_ 10': 0.34; 'there': 0.35; 'really': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'track': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'simple': 0.61; 'content-disposition:inline': 0.62; '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 Date: Sun, 26 Apr 2015 21:45:00 -0700 From: Ethan Furman To: python-list@python.org Subject: Re: Function decorator having arguments is complicated Mail-Followup-To: python-list@python.org References: <20150427042426.GD32422@stoneleaf.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430109903 news.xs4all.nl 2879 [2001:888:2000:d::a6]:47689 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89442 On 04/27, Chris Angelico wrote: > On Mon, Apr 27, 2015 at 2:24 PM, Ethan Furman 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. I find it much easier to keep track of what is going on, especially in those cases where there is pre-, post-, or both, processing going on. YMMV. -- ~Ethan~