Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29230
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2012-09-15 07:18 -0700 |
| References | (2 earlier) <k3043s$pj$1@ger.gmane.org> <mailman.722.1347657439.27098.python-list@python.org> <5053c030$0$29981$c3e8da3$5496439d@news.astraweb.com> <a6de2f24-c9af-4193-8ce3-63743fe424be@googlegroups.com> <mailman.742.1347703468.27098.python-list@python.org> |
| Subject | Re: Decorators not worth the effort |
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
| Message-ID | <mailman.751.1347718701.27098.python-list@python.org> (permalink) |
David Hutto於 2012年9月15日星期六UTC+8下午6時04分28秒寫道: > On Sat, Sep 15, 2012 at 5:45 AM, 88888 Dihedral > > <dihedral88888@googlemail.com> wrote: > > > Steven D'Aprano於 2012年9月15日星期六UTC+8上午7時39分28秒寫道: > > >> 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 this > > >> > > >> 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 = 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 = 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. > > > > > > -- > > 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 functional analysis.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Python presentations Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-09-13 19:54 +0200
Re: Python presentations Alister <alister.ware@ntlworld.com> - 2012-09-13 18:18 +0000
Decorators not worth the effort alex23 <wuwei23@gmail.com> - 2012-09-13 18:58 -0700
Re: Decorators not worth the effort Cameron Simpson <cs@zip.com.au> - 2012-09-14 12:12 +1000
Re: Decorators not worth the effort alex23 <wuwei23@gmail.com> - 2012-09-13 20:25 -0700
Re: Decorators not worth the effort Dieter Maurer <dieter@handshake.de> - 2012-09-14 08:40 +0200
Re: Decorators not worth the effort Terry Reedy <tjreedy@udel.edu> - 2012-09-14 16:29 -0400
Re: Decorators not worth the effort Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-15 09:13 +0200
Re: Decorators not worth the effort alex23 <wuwei23@gmail.com> - 2012-09-16 17:38 -0700
Re: Decorators not worth the effort Terry Reedy <tjreedy@udel.edu> - 2012-09-14 16:31 -0400
Re: Decorators not worth the effort Terry Reedy <tjreedy@udel.edu> - 2012-09-14 16:37 -0400
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-18 16:47 -0700
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-18 16:47 -0700
Re: Decorators not worth the effort Terry Reedy <tjreedy@udel.edu> - 2012-09-14 16:33 -0400
Re: Decorators not worth the effort Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-14 15:16 -0600
Re: Decorators not worth the effort Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-14 23:39 +0000
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-15 02:45 -0700
Re: Decorators not worth the effort Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-15 06:04 -0400
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-15 07:18 -0700
Re: Decorators not worth the effort Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-18 14:16 +0200
Re: Decorators not worth the effort 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-15 07:18 -0700
Re: Decorators not worth the effort Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-14 23:42 -0400
Re: Decorators not worth the effort "Dieter Maurer" <dieter@handshake.de> - 2012-09-15 08:31 +0200
csiph-web