[Python-Dev] Re: Decorator order implemented backwards?

Jack Diederich jack at performancedrivers.com
Mon Aug 16 07:35:40 CEST 2004


On Mon, Aug 16, 2004 at 12:38:06AM -0400, James Y Knight wrote:
> On Aug 15, 2004, at 11:45 PM, Jack Diederich wrote:
> >My patch to add support for class decorators defines func before the
> >decorators as a side effect of moving Mark's decorator code out of
> >com_funcdef (and yes, that was a plug).
> 
> That's definitely worse. It causes a possibly incorrect temporary value 
> to be bound, even if only for a short amount of time. The current 
> behavior of only doing the store after executing the decorators is 
> cleaner.

I agree it would be unexpected if a function defined two lines down 
happens before the @mutate above it.

> I'd rewrite the translation as:
> """
> The following code:
> 	@f1(arg)
> 	@f2
> 	def func(): return True
> 
>  is nearly* equivalent to:
> 	func = f1(arg)(f2(lambda : True))
> 
> * However, you don't have the syntactical restrictions of a lambda, and 
> the function name gets set to "func" instead of "<lambda>".
> """

Yes, the docs just need some kind of asterisk about the evaluation order.
I was just pointing out a minor documentation bug.  Mainly I just wanted to
say "class decorators please" and to say I've submitted and would be happy
to submit more patches to make it happen (for '@' or anything else).
I currently write:

class FooOverTimeReport(object):
  __metaclass__ = RunDaily # works OK unless you need more than one
  ...

when I mean

@run_daily
class FooOverTimeReport(object):
  ...


-Jack


More information about the Python-Dev mailing list