[Python-Dev] dealing with decorators hiding metadata of decorated functions

Brett Cannon brett at python.org
Sat Mar 18 00:44:16 CET 2006

With the discussion of a possible @decorator to help set the metadata
of the decorator to that of what the wrapped function has, I had an
idea that I wanted to toss out there (this dicussion stems from a blog
post I made: http://sayspy.blogspot.com/2006/03/how-to-handle-object-identity-issues.html).

The problem I have with the current practice of setting __name__ and
__doc__ (updating __dict__ I have no problem with) is that is destroys
metadata on the decorator.  This seems kind of bad to destroy metadata
for introspection purposes.

So how about a __decorates__ attribute that points to what something
is decorating?  Then help() along with 'inspect' can be changed to
look for that attribute, and if it is there, use that for
introspection.  This way advanced users can still poke at the
decorator for information while more common introspection will still
point to the decorated function instead of the decorator itself, all
without data loss!  And on top of it all, it's simple to add support
for (obviously the actual code in 'inspect' would be different than
what is below  =) !::

 _help = help

 def help(obj):
      while hasattr(obj, "__decorates__"):
         obj = obj.__decorates__

Worst case is advanced users at the command line will be put out by
not being able to directly look at obj.__doc__ for the docstring they
care about and such.  But this should be a minor issue.

But if people don't like that, I do have another proposal to add a
__signature__ attribute that would hold an object representing the
call signature of a function.  This could then be put on a decorator
so that introspection on the calling requirements of a decorated
function are correct instead of the typical ``*args, **kwargs``.

Anyway, what do people think of a __decorates__ attribute?


More information about the Python-Dev mailing list