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


Groups > comp.lang.python > #89444

Re: Function decorator having arguments is complicated

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
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; 'python.': 0.02; 'programmer': 0.03; 'parameters': 0.04; 'syntax': 0.04; 'argument': 0.05; 'interpreter': 0.05; '%s"': 0.09; 'arguments': 0.09; 'correct,': 0.09; 'currently,': 0.09; 'decorator': 0.09; 'function,': 0.09; 'parsing': 0.09; 'pep': 0.09; 'sentence': 0.09; 'subject:Function': 0.09; 'subset': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; '"modify"': 0.16; '**kwds)': 0.16; '**kwds):': 0.16; 'callable': 0.16; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hard- coded': 0.16; 'introduces': 0.16; 'simplest': 0.16; 'url:peps': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'module': 0.19; 'seems': 0.21; '(the': 0.22; 'example': 0.22; 'programming': 0.22; 'saying': 0.22; 'to:name:python- list@python.org': 0.22; 'decorators': 0.24; 'propose': 0.24; 'url:dev': 0.24; 'mon,': 0.24; 'mention': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'specifically': 0.29; 'chris': 0.29; '[1]': 0.29; 'related': 0.29; "doesn't": 0.30; 'strongly': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'easier': 0.31; 'that.': 0.31; 'usually': 0.31; 'assert': 0.31; 'proposing': 0.31; 'restricted': 0.31; 'there.': 0.32; 'extend': 0.32; 'another': 0.32; 'up.': 0.33; 'url:python': 0.33; 'link.': 0.33; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'agree': 0.35; 'common': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'described': 0.36; 'useful': 0.36; 'url:org': 0.36; 'too': 0.37; 'list': 0.37; 'problems': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'expression': 0.60; 'hope': 0.61; 'introduced': 0.61; 'simply': 0.61; 'simple': 0.61; 'further': 0.61; 'skip:n 10': 0.64; 'teaching': 0.64; 'different': 0.65; 'life': 0.66; 'here': 0.66; 'nobody': 0.68; 'results': 0.69; 'guaranteed': 0.75; 'introduce': 0.78; '(always': 0.84; '2015': 0.84; 'maybe,': 0.84; 'proposal,': 0.91; 'this;': 0.91; 'students,': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=sXGm+/tefgMX9BX4zgyePQBM/mxySOUK4YK0zaQgZdQ=; b=AV2irbBKfYUGfLkBzCkNqPvinU6bDwefu6tsSubKJTNYaiUk+M+CyOeJmC0G2TAe84 tNZiRToi2uNhACfI79De1g0guKJOA4NesXrxCFn8lCBdeu+GLJTlcCqPM2jYNBnItkWT /yBxN388TyZmD4pIYY8PXAPwq8XFOHVHT3aTHBJyy3J2JFIStDXZ+wl0keZc0L6aSHra d/ke94+LBqcg2cuIqt6oX3Frn1unWYntE2qd4AQId766Kt3ADXjBPSvGO6K8x8va9uHz 4zSIzRgApwHw1a/G8vtD7O8d7WlhzKGOtQ1da/BET40RzefBrJD7VKW8c6c3YmYKuCsr zYHA==
MIME-Version 1.0
X-Received by 10.50.176.137 with SMTP id ci9mr11367965igc.2.1430112636405; Sun, 26 Apr 2015 22:30:36 -0700 (PDT)
In-Reply-To <CAFTm5RuzO4jKZWpoYsexze7w-GVbUDSGQB=J8eAeY0ZDOY-Uow@mail.gmail.com>
References <CAFTm5Ru5NbFOtLPoVSPujeakrGawdHXjNkWJOVs1XrvBNnOUOQ@mail.gmail.com> <CAPTjJmqruXnxm=WOF36Kh56hprnxMkHfXKOv8cMasGn-CEusbg@mail.gmail.com> <CAFTm5RuzO4jKZWpoYsexze7w-GVbUDSGQB=J8eAeY0ZDOY-Uow@mail.gmail.com>
Date Mon, 27 Apr 2015 15:30:36 +1000
Subject Re: Function decorator having arguments is complicated
From Chris Angelico <rosuav@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.38.1430112639.3680.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430112639 news.xs4all.nl 2857 [2001:888:2000:d::a6]:53736
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89444

Show key headers only | View raw


(Redirecting to the list - hope you don't mind)

On Mon, Apr 27, 2015 at 2:36 PM, Makoto Kuwata <kwa@kuwata-lab.com> wrote:
>
>
> On Mon, Apr 27, 2015 at 12:20 PM, Chris Angelico <rosuav@gmail.com> wrote:
>>
>>
>> I agree it would be nice to have extra parameters directly handled,
>> but before you go further with the proposal, I suggest having a read
>> of the original PEP that introduced decorators:
>>
>> https://www.python.org/dev/peps/pep-0318/
>>
>
> Thanks, it is an useful link.
> But I can't find any sentence related to the problem what I argued.

Correct, it doesn't specifically say you can't propose this; I'm just
saying it's good to know how we got here before proposing to go
further.

> I found the following example code in PEP318:
>
>> def accepts(*types):
>>     def check_accepts(f):
>>         assert len(types) == f.func_code.co_argcount
>>         def new_f(*args, **kwds):
>>             for (a, t) in zip(args, types):
>>                 assert isinstance(a, t), \
>>                        "arg %r does not match %s" % (a,t)
>>             return f(*args, **kwds)
>>         new_f.func_name = f.func_name
>>         return new_f
>>     return check_accepts
>
> But nobody seems to mention the complication of the above code.
>
>
>>
>> There are a *lot* of different ways that decorators could have been
>> done. Currently, decorators use a restricted subset of expression
>> syntax; the bit after the @ is guaranteed [1] to be a valid expression
>> which results in a callable which is passed one argument (the
>> function) and whose return value replaces the function. What you
>> propose would no longer be a valid expression, and may introduce
>> parsing problems for the interpreter and/or for humans.
>
>
> Do you mean that it is too difficult to extend python syntax?

Yes; not impossible, but any change to syntax has to be very strongly justified.

>> But if the writing of decorators is hard and common for you, you can
>> always use a bit of metaprogramming to simplify it.
>
>
> It is not so hard for me to read/write complicated decorator,
> but it is hard for young Python programmer those who I'm teaching Python.
> (And it is hard for me to explain them about complicated decorator.)
>
> It would be very good for both me and young Python programmer
> if decorator having arguments could be described in non-complicated code.
>
> # Meta decorator programming is not the solution I hope.
> # It introduces another complex.

Maybe, but you could just give your students a black-box module that
makes life easier for them - the "modify" or "decorator" function
could be in there.

Or you could do what I do with my students, and simply start from the
simplest decorators and build up. It's usually not hard to describe a
simple hard-coded decorator (always does the same thing), then a
simple wrapping decorator (calls the original function, returns a
wrapper), and then add parameters after that.

ChrisA

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


Thread

Re: Function decorator having arguments is complicated Chris Angelico <rosuav@gmail.com> - 2015-04-27 15:30 +1000

csiph-web