Function metadata (like Java annotations) in Python

O

oripel

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.
 
B

bearophileHUGS

oripel:

Maybe this is a silly suggestion, the docstring is already overloaded,
but it may be used for this too:

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

(Or somethins similar without the @). Later you can retrive the
attributes from the docstring, for example using a verbose RE like:
r"\s* @ATTR \s+ (\w*) \s* = \s* (.*)"
And you use it to create a dict of attributes.
The decorators you apply to foo() must keep its docstring too.

Bye,
bearophile
 
F

fumanchu

oripel said:
I'm trying to attach some attributes to functions and methods, similar
to Java annotations and .NET attributes.
...
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?
...
Any thoughts? Am I rehashing something old that my search skills didn't
uncover?

There are past discussions about this; google for "python-dev decorator
metadata". For example:
http://thread.gmane.org/gmane.comp.python.devel/77506/focus=77507


Robert Brewer
System Architect
Amor Ministries
(e-mail address removed)
 
P

Paddy

oripel said:
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.
 
O

oripel

Thanks bearophile,

I prefer not to use docstrings for metadata.

1. Not interfering with the other accepted docstring uses may be
difficult (doctests, epydoc)

2. It's impractical for attributes generated by code:

@attr(reference_profile_stats=pstats.Stats("foo.profile"))
def foo():
...

Regards,
Ori.
 
O

oripel

Thanks!

Now I see it's accepted to assume nice decorators that update __dict__.
At least until __decorates__ or something similar is added...
 
O

oripel

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top