Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'modify': 0.05; '*args,': 0.07; 'arguments': 0.07; 'decorator': 0.07; 'function,': 0.07; 'nested': 0.07; 'work!': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'exiting': 0.09; 'function:': 0.09; 'logic': 0.09; 'lost.': 0.09; 'sep': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; 'receives': 0.13; 'library': 0.15; 'sat,': 0.15; '*can*': 0.16; 'c):': 0.16; 'email addr:functools.wraps(func)': 0.16; 'fancy': 0.16; 'function"': 0.16; 'functools': 0.16; 'inner(*args,': 0.16; 'lambda': 0.16; 'recipe': 0.16; 'wrapped:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'certainly': 0.17; 'module': 0.19; 'import': 0.21; 'supposed': 0.21; 'anonymous': 0.22; 'decorators': 0.22; 'doc': 0.22; 'simpler': 0.22; 'work,': 0.22; '15,': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'am,': 0.27; 'separate': 0.27; 'functions.': 0.27; 'skip:@ 10': 0.27; 'all.': 0.28; 'ball': 0.29; 'email name:': 0.29; 'research.': 0.29; 'steven': 0.29; 'this.': 0.29; 'maybe': 0.29; 'fri,': 0.30; 'function': 0.30; 'could': 0.32; 'print': 0.32; 'function.': 0.33; 'utility': 0.33; 'yours,': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'clear': 0.35; 'needed': 0.35; 'from:addr:googlemail.com': 0.35; 'mapping': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'flow': 0.36; 'functional': 0.36; 'should': 0.36; 'keeps': 0.37; 'one,': 0.37; 'does': 0.37; 'two': 0.37; 'quite': 0.37; 'rather': 0.37; 'received:209': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'object': 0.38; 'some': 0.38; 'instead': 0.39; 'skip:" 10': 0.40; 'end': 0.40; 'think': 0.40; 'your': 0.60; 'easy': 0.60; 'skip:u 10': 0.60; 'real': 0.61; 'is.': 0.62; 'personal': 0.62; 'necessarily': 0.63; 'more': 0.63; 'realise': 0.65; 'analysis': 0.70; 'friendly': 0.71; 'saw': 0.75; 'you:': 0.75; '(your': 0.84; 'actually,': 0.84; 'existence.': 0.84; 'hunt': 0.84; 'moves': 0.84; 'approach.': 0.91; 'dare': 0.93; 'magical': 0.93 Newsgroups: comp.lang.python Date: Sat, 15 Sep 2012 07:18:18 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: <20120914021211.GA15642@cskk.homeip.net> <5053c030$0$29981$c3e8da3$5496439d@news.astraweb.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 123.192.32.215 MIME-Version: 1.0 Subject: Re: Decorators not worth the effort From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, 88888 Dihedral X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 351 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347718701 news.xs4all.nl 6926 [2001:888:2000:d::a6]:49089 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29230 David Hutto=E6=96=BC 2012=E5=B9=B49=E6=9C=8815=E6=97=A5=E6=98=9F=E6=9C=9F= =E5=85=ADUTC+8=E4=B8=8B=E5=8D=886=E6=99=8204=E5=88=8628=E7=A7=92=E5=AF=AB= =E9=81=93=EF=BC=9A > On Sat, Sep 15, 2012 at 5:45 AM, 88888 Dihedral >=20 > wrote: >=20 > > Steven D'Aprano=E6=96=BC 2012=E5=B9=B49=E6=9C=8815=E6=97=A5=E6=98=9F=E6= =9C=9F=E5=85=ADUTC+8=E4=B8=8A=E5=8D=887=E6=99=8239=E5=88=8628=E7=A7=92=E5= =AF=AB=E9=81=93=EF=BC=9A >=20 > >> On Fri, 14 Sep 2012 15:16:47 -0600, Ian Kelly wrote: >=20 > >> >=20 > >> >=20 > >> >=20 > >> > If only there were a conceptually simpler way to do this. Actually, >=20 > >> >=20 > >> > there is. I give you: muman than humanetadecorators! >=20 > >> >=20 > >> [code snipped but shown below] >=20 > >> >=20 > >> > Which I think is certainly easier to understand than the nested >=20 > >> >=20 > >> > functions approach. >=20 > >> >=20 > >> >=20 > >> >=20 > >> Maybe for you, but to me it is a big ball of mud. I have no idea how t= his >=20 > >> >=20 > >> is supposed to work! At a quick glance, I would have sworn that it >=20 > >> >=20 > >> *can't* work, since simple_decorator needs to see multiple arguments b= ut >=20 > >> >=20 > >> only receives one, the function to be decorated. And yet it does work: >=20 > >> >=20 > >> >=20 > >> >=20 > >> py> from functools import partial >=20 > >> >=20 > >> py> def make_wrapper(wrapper): >=20 > >> >=20 > >> ... return lambda wrapped: partial(wrapper, wrapped) >=20 > >> >=20 > >> ... >=20 > >> >=20 > >> py> @make_wrapper >=20 > >> >=20 > >> ... def simple_decorator(func, *args, **kwargs): >=20 > >> >=20 > >> ... print "Entering decorated function" >=20 > >> >=20 > >> ... result =3D func(*args, **kwargs) >=20 > >> >=20 > >> ... print "Exiting decorated function" >=20 > >> >=20 > >> ... return result >=20 > >> >=20 > >> ... >=20 > >> >=20 > >> py> @simple_decorator >=20 > >> >=20 > >> ... def my_function(a, b, c): >=20 > >> >=20 > >> ... """Doc string""" >=20 > >> >=20 > >> ... return a+b+c >=20 > >> >=20 > >> ... >=20 > >> >=20 > >> py> my_function(1, 2, 3) >=20 > >> >=20 > >> Entering decorated function >=20 > >> >=20 > >> Exiting decorated function >=20 > >> >=20 > >> 6 >=20 > >> >=20 > >> >=20 > >> >=20 > >> So to me, this is far more magical than nested functions. If I saw thi= s >=20 > >> >=20 > >> in t requires me to hunt through your library for the "simple function >=20 > >> >=20 > >> buried in a utility module somewhere" (your words), instead of seeing >=20 > >> >=20 > >> everything needed in a single decorator factory function. It requires >=20 > >> >=20 > >> that I understand how partial works, which in my opinion is quite tric= ky. >=20 > >> >=20 > >> (I never remember how it works or which arguments get curried.) >=20 > >> >=20 > >> >=20 > >> >=20 > >> And the end result is that the decorated function is less debugging- >=20 > >> >=20 > >> friendly than I demand: it is an anonymous partial object instead of a >=20 > >> >=20 > >> named function, and the doc string is lost. And it is far from clear t= o >=20 > >> >=20 > >> me how to modify your recipe to use functools.wraps in order to keep t= he >=20 > >> >=20 > >> name and docstring, or even whether I *can* use functools.wraps. >=20 > >> >=20 > >> >=20 > >> >=20 > >> I dare say I could answer all those questions with some experimentatio= n >=20 > >> >=20 > >> and research. But I don't think that your "metadecorator" using partia= l >=20 > >> >=20 > >> is *inherently* more understandable than the standard decorator approa= ch: >=20 > >> >=20 > >> >=20 > >> >=20 > >> def simple_decorator2(func): >=20 > >> >=20 > >> @functools.wraps(func) >=20 > >> >=20 > >> def inner(*args, **kwargs): >=20 > >> >=20 > >> print "Entering decorated function" >=20 > >> >=20 > >> result =3D func(*args, **kwargs) >=20 > >> >=20 > >> print "Exiting decorated function" >=20 > >> >=20 > >> return result >=20 > >> >=20 > >> return inner >=20 > >> >=20 > >> >=20 > >> >=20 > >> This is no more complex than yours, and it keeps the function name and >=20 > >> >=20 > >> docstring. >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> >=20 > >> > Parameterized decorators are not much more >=20 > >> >=20 > >> > difficult this way. This function: >=20 > >> >=20 > >> [snip code] >=20 > >> >=20 > >> > And now we have a fancy parameterized decorator that again requires = no >=20 > >> >=20 > >> > thinking about nested functions at all. >=20 > >> >=20 > >> >=20 > >> >=20 > >> Again, at the cost of throwing away the function name and docstring. >=20 > >> >=20 > >> >=20 > >> >=20 > >> I realise that a lot of this boils down to personal preference, but I >=20 > >> >=20 > >> just don't think that nested functions are necessarily that hard to >=20 > >> >=20 > >> grasp, so I prefer to see as much of the decorator logic to be in one >=20 > >> >=20 > >> place (a nested decorator function) rather than scattered across two >=20 > >> >=20 > >> separate decorators plus partial. >=20 >=20 >=20 > Like chi fu, allow decorators to evolve upon themselves. Like simple >=20 > moves flow through water and allow memorization of activity through >=20 > evidence of existence. >=20 >=20 >=20 >=20 >=20 > --=20 >=20 > Best Regards, The concept of decorators is just a mapping from a function to another function with the same name in python. It should be easy to be grapsed for those studied real analysis and=20 functional analysis. =20