Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'none:': 0.07; 'arguments,': 0.09; 'decorator': 0.09; 'ex:': 0.09; 'idea?': 0.09; 'subject:Function': 0.09; 'wrapper': 0.09; 'python': 0.11; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'arg2,': 0.16; 'func': 0.16; 'heap': 0.16; 'notation': 0.16; 'optional': 0.16; 'y):': 0.16; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.16; 'to:name:python-list@python.org': 0.22; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'function': 0.29; '[1]': 0.29; 'cool': 0.30; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; 'quite': 0.32; 'could': 0.34; 'problem': 0.35; 'definition': 0.35; 'received:google.com': 0.35; 'skip:j 20': 0.36; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'solve': 0.60; 'skip:n 10': 0.64; 'more': 0.64; '2015': 0.84; 'message)': 0.84; 'n):': 0.84; 'amongst': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-type; bh=t8FV1iSPJYg25OSN5ivnIwaLLdRR0h06FX2yE5uLIvw=; b=c1rJPS9RiDaRbGwb+9MUrUoQtT4fUJ5NWqReiVaI5B/u8TGC1+lEsNmPYgSZ1sOC+4 l2Cwb3ofAhRA8wYgJJjtj98WRQv+F4e122FQhO2pUiOu2GCTEoLX5Zl7UqCGeyJZbevl uh3E/sYx6RdP6/WYjltEvYm9iqutYzcf2zyhM4HWNfQ84ICRPHD8yZgTwTBNVkfb1R19 dIsYvtblYZ1wcOnUX/KUDyEFrkhlSsQ80IU4h340dj4vkdHpmNkn3qrO6R5cVIaXMgGR MPADRCVNQJQkKnuGOA32+8MGLZYsJPn9zfzudPFc+OXDKCkD5l2b2fQvutbhVa/ADRuy 9+IA== X-Received: by 10.112.235.133 with SMTP id um5mr10647512lbc.7.1430145737613; Mon, 27 Apr 2015 07:42:17 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Maxime S Date: Mon, 27 Apr 2015 14:42:17 +0000 Subject: Re: Function decorator having arguments is complicated To: Makoto Kuwata , "python-list@python.org" Content-Type: multipart/alternative; boundary=001a11c3cc90aae8510514b5c041 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: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430145739 news.xs4all.nl 2891 [2001:888:2000:d::a6]:60299 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89460 --001a11c3cc90aae8510514b5c041 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Le lun. 27 avr. 2015 =C3=A0 04:39, Makoto Kuwata a =C3= =A9crit : > > 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? > David Beazley has a nice trick [1] to allow optional argument in decorators= : def logged(func=3DNone, level=3Dlogging.DEBUG, message=3DNone): if func is None: return partial(logged, level=3Dlevel, message=3Dmessage) @wraps(func) def wrapper(*args, **kwargs): log.log(level, message) return func(*args, **kwargs) return wrapper I think that solve your problem nicely, and that it is quite readable. [1] Amongst a heap of other cool tricks, in his Python Cookbook Regards, Maxime --001a11c3cc90aae8510514b5c041 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Le=C2=A0lun. 27 avr. 2015 =C3= =A0=C2=A004:39, Makoto Kuwata <kwa= @kuwata-lab.com> a =C3=A9crit=C2=A0:
<= div dir=3D"ltr">
If function decorator notation could take arguments,decorator definition would be more simple:

=C2=A0 def multiply(fun= c, n):
=C2=A0=C2=A0=C2=A0 def newfunc(*args, **kwargs):
=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 return n * func(*args, **kwargs)
=C2=A0=C2=A0=C2=A0 r= eturn newfunc

=C2=A0 @multiply 4=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 # ex:= @decorator arg1, arg2, arg3
=C2=A0 def f1(x, y):
=C2=A0=C2=A0=C2=A0 = return x+y


How do you think about this idea?

David Beazley has a nice trick [1] to allow op= tional argument in decorators:
=

def logged(func=3DNone, level=3Dlogg= ing.DEBUG, message=3DNone):
=C2=A0 =C2=A0 if func is None:
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return partial(logged, level=3Dlevel, messa= ge=3Dmessage)

=C2=A0 =C2=A0 @wraps(func)
=C2=A0 =C2=A0 def wrapper(*args, **kwargs):
=C2=A0 =C2=A0 =C2=A0= =C2=A0 log.log(level, message)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 retur= n func(*args, **kwargs)
=C2=A0 =C2=A0 return wrapper
<= div>
I think that solve your problem nicely, and that it is q= uite readable.=C2=A0

[1] Amongst a heap of other c= ool tricks, in his Python Cookbook

Regards,
<= div>
Maxime
--001a11c3cc90aae8510514b5c041--