Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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; 'arguments': 0.09; 'arguments,': 0.09; 'decorator': 0.09; 'ex:': 0.09; 'idea?': 0.09; 'subject:Function': 0.09; 'python': 0.11; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'arg2,': 0.16; 'complicated,': 0.16; 'notation': 0.16; 'y):': 0.16; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.16; 'sender:addr:gmail.com': 0.17; 'to:name:python-list@python.org': 0.22; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'could': 0.34; 'definition': 0.35; 'received:google.com': 0.35; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'experts': 0.60; 'skip:\xc2 10': 0.60; 'skip:n 10': 0.64; 'more': 0.64; '8bit%:100': 0.72; 'n):': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:date:message-id:subject:from:to:content-type; bh=E706VidcWJeQ4+IDIYrwVGblwKj5ASQQ93yCAOLRDog=; b=a4Y6uhUoDQtXmkJnm/epXzSf6SuBUtG7Xkc25DOuH8+jVjCN1DPipl68GzLHWSifgm S8M1s0hVRohvEfT0R409EQAR0Ipkhh3gq1x5w3Oj+6UkpkU+M9BPr+b7mpfFomHEJiZU i9LI6TgZ5Xw9bsDSh6xqKNUUdudOlmXJsjSL8hv6xhw82M2fCzh0RD3PT+60D1YcGNj6 FEJmPf66pjd1c/excplH/oGVDok1stmBwH5E1jqKr/rsuKpc+mKXiMaUq95CHuSlHWj2 sMXvpdIs3N5YSXxMPDCoNg+2YObjBUkggqi8gWYCvsIhREasmXBHC5NcI3K55tz68A2J PDzA== MIME-Version: 1.0 X-Received: by 10.107.128.3 with SMTP id b3mr11223710iod.24.1430102267825; Sun, 26 Apr 2015 19:37:47 -0700 (PDT) Sender: kwatch@gmail.com Date: Mon, 27 Apr 2015 11:37:47 +0900 X-Google-Sender-Auth: S_LMi8PmTVBG_yC9CbLNJPf8fYs Subject: Function decorator having arguments is complicated From: Makoto Kuwata To: "python-list@python.org" Content-Type: multipart/alternative; boundary=001a113f964caa94890514aba118 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: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430102277 news.xs4all.nl 2861 [2001:888:2000:d::a6]:59662 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5265 X-Received-Body-CRC: 3988324711 Xref: csiph.com comp.lang.python:89438 --001a113f964caa94890514aba118 Content-Type: text/plain; charset=UTF-8 I want to ask Python experts about function decorator which has arguments. 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 @multiply(4) def f1(x, y): return x+y print(f1(2, 3)) #=> 20 (= 4 * (2+3)) 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? -- regards, makoto kuwata --001a113f964caa94890514aba118 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I want to ask Python experts about function decorator= which has arguments.

I feel that function decorator having argument= s is complicated,
because three 'def' are nested:

=C2=A0 = def multiply(n):
=C2=A0=C2=A0=C2=A0 def deco(func):
=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 def newfunc(*args, **kwargs):
=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 return n * func(*args, **kwargs)
=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 return newfunc
=C2=A0=C2=A0=C2=A0 return deco

=C2=A0= @multiply(4)
=C2=A0 def f1(x, y):
=C2=A0=C2=A0=C2=A0 return x+y
<= br>=C2=A0 print(f1(2, 3))=C2=A0=C2=A0 #=3D> 20=C2=A0 (=3D 4 * (2+3))
=

If function decorator notation could take arguments,
decorator d= efinition would be more simple:

=C2=A0 def multiply(func, 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 return newfun= c

=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?

--
regards= ,
makoto kuwata
--001a113f964caa94890514aba118--