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


Groups > comp.lang.python > #111020

Re: Meta decorator with parameters, defined in explicit functions

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Meta decorator with parameters, defined in explicit functions
Date Mon, 4 Jul 2016 09:39:35 +1000
Lines 54
Message-ID <mailman.49.1467589185.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> <mailman.15.1467436194.2295.python-list@python.org> <facf7142-25f5-4a8b-ab37-771884ab8d64@googlegroups.com> <CALwzid=GfLU8nbc5m4M9FxEO1ObWDTcvLJ3-wJXS0=w88-v8sw@mail.gmail.com> <mailman.33.1467521333.2295.python-list@python.org> <46690e10-1534-4f99-a40f-a148b4a21a27@googlegroups.com> <CALwzidmn-0TyxfKxcZoNoV5CgiFuOqRS_X=W34gSn5a-NVOy_w@mail.gmail.com> <mailman.38.1467546815.2295.python-list@python.org> <d2f71d72-d2da-4f68-9d6a-ca059327afdd@googlegroups.com> <CALwzid=ZsBnqjcMHVU6YQ17_993z01K3wKZ2QwZb9LKF_KJt_A@mail.gmail.com> <mailman.47.1467585556.2295.python-list@python.org> <62b43f29-e6ea-4d19-9e9f-1becb3988bd3@googlegroups.com> <CAPTjJmos8NLWnNr2AcmjK6RqFFSknGqMzBfuTScxHvwd46fCDg@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 BvXuWK9kLH8eHoBA5XcIfAegvlVvUNdUtMJA/tOzZXpA==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'modified': 0.05; '*args,': 0.07; '*not*': 0.07; 'caller': 0.07; 'correct.': 0.07; 'cc:addr :python-list': 0.09; '*args': 0.09; 'caller.': 0.09; 'immutable': 0.09; 'def': 0.13; 'argument': 0.15; '*i*': 0.16; '2016': 0.16; 'called,': 0.16; 'constructs': 0.16; 'docstring': 0.16; 'exhausted': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'letters.': 0.16; 'namespace.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sense,': 0.16; 'server"""': 0.16; 'subject:parameters': 0.16; 'uppercase': 0.16; 'usage,': 0.16; 'wrote:': 0.16; 'string': 0.17; '"you': 0.18; 'string,': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'not,': 0.22; 'function,': 0.22; 'lawrence': 0.22; 'sorry,': 0.22; 'am,': 0.23; 'replacing': 0.23; 'specified': 0.23; 'header:In- Reply-To:1': 0.24; 'mon,': 0.24; "doesn't": 0.26; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'see,': 0.27; 'correct': 0.28; 'function': 0.28; '**kwargs)': 0.29; '*the': 0.29; 'docstrings': 0.29; 'convert': 0.29; 'raise': 0.29; 'generally': 0.32; 'returned': 0.32; 'received:google.com': 0.35; 'ones': 0.35; 'but': 0.36; 'should': 0.36; 'assigned': 0.36; 'monday,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'expect': 0.37; 'setting': 0.37; 'say': 0.37; 'seem': 0.37; 'why': 0.39; 'whatever': 0.39; 'subject:with': 0.40; 'here:': 0.63; 'skip:\xe2 10': 0.70; 'jul': 0.72; '**kw)': 0.84; '**kw):': 0.84; 'chrisa': 0.84; 'decorator,': 0.84; 'yet?': 0.84; 'to:none': 0.91; 'technically': 0.91
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:cc :content-transfer-encoding; bh=XPM9d4qcE+TjgA47WrVyEy/SPVQpwAtyWfNVn4BplWw=; b=MJh70KapPsY7xW8zUurY/JAKouHs4NKFtlDaVus+/P6nqCBHLZIGBuxgkEtmAgKpZz t2XilLxY2fd7M6PJnwGBxnmhMp7Fl+1B63wU+CVES9kPjfBeb3Qf2ZSJmzsm3pWAzjwE mPb4nf2mpJyVJuqmbBLJFEHEmQ9QiI0iVDi1VQ8sftUFOHEWPqIy2ioxly6+TfkWCSmP kVoWT4XWOO6mZ8T1Vfw/pSUVIuOhCL5U5WGZiVcx+JNgviEpSwcRrXrWdrF3v63CJmy0 AdFKpCAvUB9WcPBD/id/Bjs41mYOxLPq+BfMVJR9lirvmIW4TzKGhvYq/NdFTfGTJb3z U8HQ==
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:cc:content-transfer-encoding; bh=XPM9d4qcE+TjgA47WrVyEy/SPVQpwAtyWfNVn4BplWw=; b=C7tVQiizu/QOi7mUKrZLtJpoKnYIJY62rh8pKp6FN6HBzrY7B8FcN8pXprxVtb3d7T z4H6Hj9sM5Ccf1p87u5ocr/zt0FbBfdTFmpwraxmA4wBMQ9bRyRJjwaDnHavIPVRVmMQ AZtcPal4S2Rks4Xt3KXGVz7EgV9N3WAZdD8+x3g9Qyi77Y60gsh2Ta+j7Ks4m9/p/QQN LXKZ+MkpT92wK+PbkCGzbG/jlcn5sMBm+FIWufD5b4+Nz+L7bt2qBXcG92S5j4UqOguZ Gm8dguXhOGiL1CWEhc4TDOViVKtvz7CQEvDADjpXL1xfQ93sRXHOBgvrlblUplGjpDpX R0MQ==
X-Gm-Message-State ALyK8tIC4tE490n2RUekG01kCkzktJLez3BXX0ktDsHKd4INRjh257AOtLmnp9cabo4rIW/G0KwiHQ6INujCBw==
X-Received by 10.28.41.195 with SMTP id p186mr8106699wmp.41.1467589176495; Sun, 03 Jul 2016 16:39:36 -0700 (PDT)
In-Reply-To <62b43f29-e6ea-4d19-9e9f-1becb3988bd3@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 <CAPTjJmos8NLWnNr2AcmjK6RqFFSknGqMzBfuTScxHvwd46fCDg@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> <CALwzidn8DuSo-L2UQTJ7wZfzK+eHGzgGuy_ZfHFR9v4J_NHnjQ@mail.gmail.com> <mailman.15.1467436194.2295.python-list@python.org> <facf7142-25f5-4a8b-ab37-771884ab8d64@googlegroups.com> <CALwzid=GfLU8nbc5m4M9FxEO1ObWDTcvLJ3-wJXS0=w88-v8sw@mail.gmail.com> <mailman.33.1467521333.2295.python-list@python.org> <46690e10-1534-4f99-a40f-a148b4a21a27@googlegroups.com> <CALwzidmn-0TyxfKxcZoNoV5CgiFuOqRS_X=W34gSn5a-NVOy_w@mail.gmail.com> <mailman.38.1467546815.2295.python-list@python.org> <d2f71d72-d2da-4f68-9d6a-ca059327afdd@googlegroups.com> <CALwzid=ZsBnqjcMHVU6YQ17_993z01K3wKZ2QwZb9LKF_KJt_A@mail.gmail.com> <mailman.47.1467585556.2295.python-list@python.org> <62b43f29-e6ea-4d19-9e9f-1becb3988bd3@googlegroups.com>
Xref csiph.com comp.lang.python:111020

Show key headers only | View raw


On Mon, Jul 4, 2016 at 9:25 AM, Lawrence D’Oliveiro
<lawrencedo99@gmail.com> wrote:
> On Monday, July 4, 2016 at 10:39:30 AM UTC+12, Ian wrote:
>> Sorry, but you're the one who doesn't seem to get it. Because it's a
>> decorator, "your" function is replacing the caller's function in the
>> caller's namespace.
>
> No it is not. The replacement happens here:
>
>         def generated_decorator(func) :
>             return \
>                 decorator(func, *args, **kwargs)
>         #end generated_decorator
>
> As you can see, the function being called, “decorator”, is supplied by the caller. The function being decorated, “func”, is supplied by the caller (along with the additional *args and **kwargs). And the returned function is returned *as-is*, so whatever docstring was assigned by the caller is *NOT* “clobbered”.
>
> Once again, the only docstrings I am setting are the ones for functions that *I* generate.
>
> Do you understand that yet?

Yes, in a technical sense, you are correct. It's also technically
correct to say that str.upper() doesn't convert its argument to
uppercase - it constructs a completely new string of all uppercase
letters. But in normal usage, people expect "x = x.upper()" to return
*the same string, uppercased*. In the same way, people expect "f =
deco(f)" to return *the same function, decorated*. Technically it's
generally going to be a new function, because functions are immutable
(more-or-less), but it should broadly be the same function.

def require_login(f):
    def modified(*a, **kw):
        if not logged_in:
            return "You must be logged in to do this."
        return f(*a, **kw)
    return modified

@require_login
def fire_ze_missiles():
    raise Exhausted

@require_login
def reboot_server(server):
    """Reboot the specified server"""
    server.send_command("sudo reboot")

Tell me, should the docstring for reboot_server be "Reboot the
specified server", or not? If not, why not?

ChrisA

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