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


Groups > comp.lang.python > #89460

Re: Function decorator having arguments is complicated

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 <maximesteisel@gmail.com>
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 <CAFTm5Ru5NbFOtLPoVSPujeakrGawdHXjNkWJOVs1XrvBNnOUOQ@mail.gmail.com>
In-Reply-To <CAFTm5Ru5NbFOtLPoVSPujeakrGawdHXjNkWJOVs1XrvBNnOUOQ@mail.gmail.com>
From Maxime S <maxischmeii@gmail.com>
Date Mon, 27 Apr 2015 14:42:17 +0000
Subject Re: Function decorator having arguments is complicated
To Makoto Kuwata <kwa@kuwata-lab.com>, "python-list@python.org" <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 <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.46.1430145739.3680.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Le lun. 27 avr. 2015 à 04:39, Makoto Kuwata <kwa@kuwata-lab.com> a écrit :
>
> 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=None, level=logging.DEBUG, message=None):
    if func is None:
        return partial(logged, level=level, message=message)

    @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

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


Thread

Re: Function decorator having arguments is complicated Maxime S <maxischmeii@gmail.com> - 2015-04-27 14:42 +0000

csiph-web