Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; '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; 'received:mail-vb0-f46.google.com': 0.09; 'sep': 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; 'received:209.85.212.46': 0.18; 'module': 0.19; 'import': 0.21; 'supposed': 0.21; 'anonymous': 0.22; 'decorators': 0.22; 'doc': 0.22; 'simpler': 0.22; 'work,': 0.22; 'cc:2**0': 0.23; '15,': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'separate': 0.27; 'functions.': 0.27; 'skip:@ 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; '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; 'received:google.com': 0.34; 'clear': 0.35; 'needed': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'flow': 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; 'header:Received:5': 0.40; 'end': 0.40; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'is.': 0.62; 'personal': 0.62; 'necessarily': 0.63; 'more': 0.63; 'realise': 0.65; 'friendly': 0.71; 'saw': 0.75; 'you:': 0.75; '(your': 0.84; '8bit%:49': 0.84; 'actually,': 0.84; 'charset:big5': 0.84; 'existence.': 0.84; 'hunt': 0.84; 'moves': 0.84; 'approach.': 0.91; 'dare': 0.93; 'magical': 0.93 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 :cc:content-type:content-transfer-encoding; bh=316HFwEbV9L6KnkxMpP8L5ffi+ZK3nXpHPtNHHGdkGU=; b=Mp/pHBieGMhuKj4IUvZMYzFzQ9AknT709Ecf0SXk/kjxUCHgxvfYbvQw2/UicSNXWY DVThJnj2AtkM5KIGvHUCiRR4kkOGc4KhflH7qq24SVtenY1xh50XrbovcaTlXhVICuWa KYr4kXe9ut77v7LtgDGMyIh2qo534FpjDfXaUmw9zvu6tKQDyhPz/QUXt1aSRKrJ6ppE jw0CxTZl+9e1v29EhgW4GsDCyAc/Kl7rwXMOcpfkxuiQ9vtyZqS/VSfqJTzITJ1MFiBm VJfm+txTNQn6RBCMkTUEk5XmgkLu7LoA4lX+GEp1FK/IehOh+kSODVv0A4ndjsl2A8zB nxRA== MIME-Version: 1.0 In-Reply-To: References: <20120914021211.GA15642@cskk.homeip.net> <5053c030$0$29981$c3e8da3$5496439d@news.astraweb.com> Date: Sat, 15 Sep 2012 06:04:25 -0400 Subject: Re: Decorators not worth the effort From: Dwight Hutto To: 88888 Dihedral Content-Type: text/plain; charset=Big5 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: , Newsgroups: comp.lang.python Message-ID: Lines: 170 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347703468 news.xs4all.nl 6876 [2001:888:2000:d::a6]:51580 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29216 On Sat, Sep 15, 2012 at 5:45 AM, 88888 Dihedral wrote: > Steven D'Aprano=A9=F3 2012=A6~9=A4=EB15=A4=E9=ACP=B4=C1=A4=BBUTC+8=A4W=A4= =C87=AE=C939=A4=C028=AC=ED=BCg=B9D=A1G >> On Fri, 14 Sep 2012 15:16:47 -0600, Ian Kelly wrote: >> >> >> >> > If only there were a conceptually simpler way to do this. Actually, >> >> > there is. I give you: muman than humanetadecorators! >> >> [code snipped but shown below] >> >> > Which I think is certainly easier to understand than the nested >> >> > functions approach. >> >> >> >> Maybe for you, but to me it is a big ball of mud. I have no idea how thi= s >> >> is supposed to work! At a quick glance, I would have sworn that it >> >> *can't* work, since simple_decorator needs to see multiple arguments but >> >> only receives one, the function to be decorated. And yet it does work: >> >> >> >> py> from functools import partial >> >> py> def make_wrapper(wrapper): >> >> ... return lambda wrapped: partial(wrapper, wrapped) >> >> ... >> >> py> @make_wrapper >> >> ... def simple_decorator(func, *args, **kwargs): >> >> ... print "Entering decorated function" >> >> ... result =3D func(*args, **kwargs) >> >> ... print "Exiting decorated function" >> >> ... return result >> >> ... >> >> py> @simple_decorator >> >> ... def my_function(a, b, c): >> >> ... """Doc string""" >> >> ... return a+b+c >> >> ... >> >> py> my_function(1, 2, 3) >> >> Entering decorated function >> >> Exiting decorated function >> >> 6 >> >> >> >> So to me, this is far more magical than nested functions. If I saw this >> >> in t requires me to hunt through your library for the "simple function >> >> buried in a utility module somewhere" (your words), instead of seeing >> >> everything needed in a single decorator factory function. It requires >> >> that I understand how partial works, which in my opinion is quite tricky= . >> >> (I never remember how it works or which arguments get curried.) >> >> >> >> And the end result is that the decorated function is less debugging- >> >> friendly than I demand: it is an anonymous partial object instead of a >> >> named function, and the doc string is lost. And it is far from clear to >> >> me how to modify your recipe to use functools.wraps in order to keep the >> >> name and docstring, or even whether I *can* use functools.wraps. >> >> >> >> I dare say I could answer all those questions with some experimentation >> >> and research. But I don't think that your "metadecorator" using partial >> >> is *inherently* more understandable than the standard decorator approach= : >> >> >> >> def simple_decorator2(func): >> >> @functools.wraps(func) >> >> def inner(*args, **kwargs): >> >> print "Entering decorated function" >> >> result =3D func(*args, **kwargs) >> >> print "Exiting decorated function" >> >> return result >> >> return inner >> >> >> >> This is no more complex than yours, and it keeps the function name and >> >> docstring. >> >> >> >> >> >> > Parameterized decorators are not much more >> >> > difficult this way. This function: >> >> [snip code] >> >> > And now we have a fancy parameterized decorator that again requires no >> >> > thinking about nested functions at all. >> >> >> >> Again, at the cost of throwing away the function name and docstring. >> >> >> >> I realise that a lot of this boils down to personal preference, but I >> >> just don't think that nested functions are necessarily that hard to >> >> grasp, so I prefer to see as much of the decorator logic to be in one >> >> place (a nested decorator function) rather than scattered across two >> >> separate decorators plus partial. Like chi fu, allow decorators to evolve upon themselves. Like simple moves flow through water and allow memorization of activity through evidence of existence. --=20 Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com