[Python-Dev] Semantics of decorators?

Phillip J. Eby pje at telecommunity.com
Wed Aug 11 18:22:04 CEST 2004


At 11:05 AM 8/11/04 -0400, Edward C. Jones wrote:
>In the development docs, Python Reference Manual, 7.5, it says:
>
>A function definition may be wrapped by one or more decorator expressions.
>Decorator expressions are evaluated when the function is defined, in the
>scope that contains the function definition. The result must be a callable,
>which is invoked with the function object as the only argument. The returned
>value is bound to the function name instead of the function object. If there
>are multiple decorators, they are applied in reverse order.
>
>Does this completely describe the semantics of decorators?

It should also be noted that when the decorator expressions are applied, 
the function has not yet been bound to its name in the appropriate 
scope.  Thus, if there is a previous binding of that name in the scope, the 
decorator expression may be able to access it.

This aspect of the semantics is important for additive decorators like Ed 
Loper's 'property_getter' and 'property_setter', or my generic function 
decorators.  E.g.:

     @property_getter
     def foo(self):
         return self.__foo

     @property_setter
     def foo(self,value):
         self.__foo = value

     # etc.




>_______________________________________________
>Python-Dev mailing list
>Python-Dev at python.org
>http://mail.python.org/mailman/listinfo/python-dev
>Unsubscribe: 
>http://mail.python.org/mailman/options/python-dev/pje%40telecommunity.com



More information about the Python-Dev mailing list