Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'arguments': 0.09; 'arguments,': 0.09; 'decorator': 0.09; 'ex:': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'idea?': 0.09; 'message- id:@stoneleaf.us': 0.09; 'subject:Function': 0.09; '~ethan~': 0.09; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'arg2,': 0.16; 'class:': 0.16; 'complicated,': 0.16; 'notation': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'class': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'definition': 0.35; 'charset:us-ascii': 0.36; 'same.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'received:173': 0.61; 'content-disposition:inline': 0.62; 'skip:n 10': 0.64; 'more': 0.64; 'n):': 0.84; 'self.n': 0.84 Date: Sun, 26 Apr 2015 21:24:26 -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: 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430108682 news.xs4all.nl 2880 [2001:888:2000:d::a6]:42002 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89440 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 > @multiply(4) > def f1(x, y): > return x+y > > print(f1(2, 3)) #=> 20 (= 4 * (2+3)) This all works the same. > > > If function decorator notation could take arguments, > decorator definition would be more simple: > > def multiply(func, n): > def newfunc(*args, **kwargs): > return n * func(*args, **kwargs) > return newfunc > > @multiply 4 # ex: @decorator arg1, arg2, arg3 > def f1(x, y): > return x+y > > > How do you think about this idea? It's unnecessary, just use a class instead of a function to get more clarity. -- ~Ethan~