Function metadata (like Java annotations) in Python

oripel oripel at gmail.com
Sun Sep 10 15:05:27 CEST 2006


Thanks Paddy - you're showing normal use of function attributes.
They're still hidden when wrapped by an uncooperative decorator.

Paddy wrote:
> oripel wrote:
> > 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.
>
> I wrote up my investigation into function attributes and decorators on
> my blog:
>
> http://paddy3118.blogspot.com/2006/05/python-function-attributes.html
> http://paddy3118.blogspot.com/2006/05/function-attributes-assigned-by.html
> 
> What do you think?
> 
> - Paddy.




More information about the Python-list mailing list