Function metadata (like Java annotations) in Python

oripel oripel at gmail.com
Sun Sep 10 04:13:28 CEST 2006


Hi,

I'm trying to attach some attributes to functions and methods, similar
to Java annotations and .NET attributes.
I also want to use a convenient decorator for it, something along the
lines of

@attr(name="xander", age=10)
def foo():
  ...

Assigning attributes to the function will work, as will assigning keys
and values to a dictionary in an attribute. But if there are more
decorators in the way, this could fail:

@onedec
@attr(...)
@twodec
def foo():
  ...

Given 'foo' now, how do I find the attributes?

Assigning to a global attribute registry (some interpreter-global
dictionary), although less desirable, might have been acceptable, but
then how do I identify the function after it's been wrapped in more
decorators?

Also, it may be nice to have the following work as well:

@attr(name="Xander")
@attr(age=10)
@somedec
@attr(hobby="knitting")
def foo():
  ...


Any thoughts? Am I rehashing something old that my search skills didn't
uncover?

Thanks!
Ori.




More information about the Python-list mailing list