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


Groups > comp.lang.python > #110942

Re: Meta decorator with parameters, defined in explicit functions

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Meta decorator with parameters, defined in explicit functions
Date Fri, 1 Jul 2016 23:09:10 -0600
Lines 53
Message-ID <mailman.15.1467436194.2295.python-list@python.org> (permalink)
References <85mvm5vrbw.fsf@benfinney.id.au> <mailman.61.1467090177.2358.python-list@python.org> <21916e7b-9e19-4cef-94e0-bac331540ca2@googlegroups.com> <CALwzidn8DuSo-L2UQTJ7wZfzK+eHGzgGuy_ZfHFR9v4J_NHnjQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de RGtq1o8gHPm+CpkpXsKxJArvvn8JfpUBQu7bw3hDg6WA==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.028
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; '*args,': 0.07; 'satisfy': 0.09; 'def': 0.13; 'applies': 0.15; '2016': 0.16; 'copy-pasted': 0.16; 'lambda': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:parameters': 0.16; 'wrote:': 0.16; 'skip:" 40': 0.20; 'decorator': 0.22; 'lawrence': 0.22; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; '**kwargs)': 0.29; '**kwargs):': 0.29; 'clever': 0.29; 'code': 0.30; 'putting': 0.30; 'returned': 0.32; 'received:google.com': 0.35; 'something': 0.35; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'keyword': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:p 20': 0.38; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'bases': 0.63; 'jul': 0.72; 'decorate': 0.84; 'decorator:': 0.84; 'i\xe2\x80\x99ll': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-transfer-encoding; bh=4/T1GCroMCP2i6OLyGytfC0oZhL2HO7mkFVAhKUwWK4=; b=Sc4SvO2Vk72vjYV4JVgDwr5E6cMQHpILDt2A5XH3J4LZqxkD6vgGppJGI5IMaOiVEi a+3FeXoB1JDLlx484ceFVvywljeJ+0mQIImVC56zIgfGH+UqB/xM074/VnYLr5V3bQdh nt/F0wO6LrDljC+GHdLzQ/Gn4i8CVmKPGQYPBpeZiG7fmfiJ2rkYTFH31vUxvzK43Kh+ sQHYfPGiU6fYuOm41hbmg/fPl5lBX193KUxLYt75AWlFgy6sKCRoqB5mnaxi8I8YPUfu nIPSw3tP5IB1nGWu5BTTAW4MDCcqfdSQRoFLBzQ3t0hg7IzXBjUDGsdh7T6udDHDvIQ0 Z27Q==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-transfer-encoding; bh=4/T1GCroMCP2i6OLyGytfC0oZhL2HO7mkFVAhKUwWK4=; b=Ae7W8bvTHx+ss4vgk8AcWdnpYZ8BY/u9dFLaGVpZWZ+o6Iscy2nmIXMZp4mB/EaFIF OpY6jTlH7HRf8xxNt3jFlRbB9iRncPURb++lLBnOThlkuziSe2F/PzTvX7qjzNBT1G9N hCvDetCCH3WAdADdrcWDs38np0q16MpKg5q2bDZxLqFWNiQ7c6F3X3a3uRG2tKbcn9LJ BkXCCJaKKzxtDlKQJK3fNP3Mv6mxOfb5g8m+R5t8UfDPP+mAh9oMV8E1OGG8P9L8Fv67 cDsclc1Wcz+aUV3ODnhPfTrq7v7r9Tmr3v2a0tLZuNr0l/OunTaPmRgvUC70SIizmYut txCQ==
X-Gm-Message-State ALyK8tIyyNw/eC91jgDbefRqXfc/k3c7EDFNXTtVddJBAVSFqMflurEa6C4giu+uyn210wBppQBzFxhh5mkBUg==
X-Received by 10.157.37.6 with SMTP id k6mr1163039otb.115.1467436190354; Fri, 01 Jul 2016 22:09:50 -0700 (PDT)
In-Reply-To <21916e7b-9e19-4cef-94e0-bac331540ca2@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <CALwzidn8DuSo-L2UQTJ7wZfzK+eHGzgGuy_ZfHFR9v4J_NHnjQ@mail.gmail.com>
X-Mailman-Original-References <85mvm5vrbw.fsf@benfinney.id.au> <mailman.61.1467090177.2358.python-list@python.org> <21916e7b-9e19-4cef-94e0-bac331540ca2@googlegroups.com>
Xref csiph.com comp.lang.python:110942

Show key headers only | View raw


On Fri, Jul 1, 2016 at 4:08 PM, Lawrence D’Oliveiro
<lawrencedo99@gmail.com> wrote:
> On Tuesday, June 28, 2016 at 5:03:08 PM UTC+12, Ben Finney wrote:
>> There is a clever one-line decorator that has been copy-pasted without
>> explanation in many code bases for many years::
>>
>>     decorator_with_args = lambda decorator: lambda *args, **kwargs: lambda func: decorator(func, *args, **kwargs)
>>
>
> For those who want docstrings, I’ll give you docstrings:
>
>     def decorator_with_args(decorator) :
>         "given function decorator(func, *args, **kwargs), returns a decorator which," \
>         " given func, returns the result of decorator(func, *args, **kwargs)."
>
>         def decorate(*args, **kwargs) :
>
>             def generated_decorator(func) :
>                 return \
>                     decorator(func, *args, **kwargs)
>             #end generated_decorator
>
>         #begin decorate
>             generated_decorator.__name__ = "decorator_{}".format(decorator.__name__)
>             generated_decorator.__doc__ = "decorator which applies {} to the previously-specified arguments".format(decorator.__name__)
>             return \
>                 generated_decorator
>         #end decorate
>
>     #begin decorator_with_args
>         decorate.__name__ = "decorate_with_{}".format(decorator.__name__)
>         decorate.__doc__ = "generates a decorator which applies {} to the given arguments".format(decorator.__name__)

You should use functools.wraps instead of clobbering the decorated
function's name and docstring:

        @functools.wraps(decorator)
        def decorate(*args, **kwargs):
            ...

>         return \
>             decorate

Just to satisfy my own curiosity, do you have something against
putting the return keyword and the returned expression on the same
line?

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


Thread

Meta decorator with parameters, defined in explicit functions Ben Finney <ben+python@benfinney.id.au> - 2016-06-28 15:02 +1000
  Re: Meta decorator with parameters, defined in explicit functions Paul Rubin <no.email@nospam.invalid> - 2016-06-27 23:08 -0700
  Re: Meta decorator with parameters, defined in explicit functions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-28 18:39 +1000
  Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-29 19:43 -0700
    Re: Meta decorator with parameters, defined in explicit functions Steven D'Aprano <steve@pearwood.info> - 2016-06-30 13:49 +1000
    Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions Peter Otten <__peter__@web.de> - 2016-06-30 09:25 +0200
      Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-30 00:43 -0700
        Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-30 18:00 +1000
        Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions Peter Otten <__peter__@web.de> - 2016-06-30 10:41 +0200
    Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions dieter <dieter@handshake.de> - 2016-07-01 09:04 +0200
      Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-01 21:25 +0100
        Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions Ben Finney <ben+python@benfinney.id.au> - 2016-07-02 11:52 +1000
        Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions dieter <dieter@handshake.de> - 2016-07-02 08:57 +0200
        Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions Ben Finney <ben+python@benfinney.id.au> - 2016-07-02 18:00 +1000
  Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-29 19:50 -0700
  Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-01 15:08 -0700
    Re: Meta decorator with parameters, defined in explicit functions Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-01 23:09 -0600
      Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-01 23:40 -0700
        Re: Meta decorator with parameters, defined in explicit functions Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-02 22:48 -0600
          Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-02 22:37 -0700
            Re: Meta decorator with parameters, defined in explicit functions Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-03 05:52 -0600
              Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-03 14:17 -0700
                Re: Meta decorator with parameters, defined in explicit functions Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-03 16:38 -0600
                Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-03 16:25 -0700
                Re: Meta decorator with parameters, defined in explicit functions Chris Angelico <rosuav@gmail.com> - 2016-07-04 09:39 +1000
                Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-03 16:59 -0700
                Re: Meta decorator with parameters, defined in explicit functions Chris Angelico <rosuav@gmail.com> - 2016-07-04 10:06 +1000
                Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-03 17:23 -0700
                Re: Meta decorator with parameters, defined in explicit functions Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-03 18:01 -0600
                Re: Meta decorator with parameters, defined in explicit functions Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-07-03 17:06 -0700
                Re: Meta decorator with parameters, defined in explicit functions Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-03 18:20 -0600
    Re: Meta decorator with parameters, defined in explicit functions Ben Finney <ben+python@benfinney.id.au> - 2016-07-02 15:32 +1000
    Re: Meta decorator with parameters, defined in explicit functions Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-01 23:53 -0600

csiph-web