[Python-Dev] decorator support

Josiah Carlson jcarlson at uci.edu
Mon Sep 6 00:42:56 CEST 2004

>    newf.__doc__  = oldf.__doc__        # copy the docstring
>    newf.__dict__.update(oldf.__dict__) # copy attributes
>    newf.__name__ = oldf.__name__       # keep the name (new in Py2.4)
>    newf.__undecorated__ = oldf         # [XX NEW] ptr to undecorated obj

If one were to consider function decorators as a type of 'subclass' of a
particular function, that is, post-decoration a function inherits from
the pre-decoration version of the function, we could, with a single
attribute (like __undecorated__ or __proxyfor__ as suggested) and proper
introspection tools, do iterative attribute lookups similar to the way
that it is already done with classes (without diamond-inheritance). That
is, one wouldn't need to copy __doc__, __name__, and __dict__ from a
decorated function, one would get access to them automatically.

In current Python, what I am saying would be equivalent to...

def foo(arg):

t = foo
foo = arbitrary_decorator(foo)
foo.__proxyfor__ = t
del t

It would be some 'behind-the-scenes-magic', but I think it may be the
right amount and kind of magic.

 - Josiah

More information about the Python-Dev mailing list