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


Groups > comp.lang.python > #89438

Function decorator having arguments is complicated

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 <kwatch@gmail.com>
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 <kwa@kuwata-lab.com>
To "python-list@python.org" <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 <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.31.1430102277.3680.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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

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


Thread

Function decorator having arguments is complicated Makoto Kuwata <kwa@kuwata-lab.com> - 2015-04-27 11:37 +0900
  Re: Function decorator having arguments is complicated Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-27 17:36 +1000
    Re: Function decorator having arguments is complicated Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-04-29 21:22 +1200

csiph-web