471,337 Members | 940 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Function metadata (like Java annotations) in Python

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.

Sep 10 '06 #1
8 2562
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

Sep 10 '06 #2
oripel wrote:
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.p...06/focus=77507
Robert Brewer
System Architect
Amor Ministries
fu******@amor.org

Sep 10 '06 #3
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/0...ttributes.html
http://paddy3118.blogspot.com/2006/0...signed-by.html

What do you think?

- Paddy.

Sep 10 '06 #4
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.pr ofile"))
def foo():
...

Regards,
Ori.

be************@lycos.com wrote:
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
Sep 10 '06 #5
Thanks!

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

fumanchu wrote:
oripel wrote:
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.p...06/focus=77507
Robert Brewer
System Architect
Amor Ministries
fu******@amor.org
Sep 10 '06 #6
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/0...ttributes.html
http://paddy3118.blogspot.com/2006/0...signed-by.html

What do you think?

- Paddy.
Sep 10 '06 #7
oripel wrote:
Thanks Paddy - you're showing normal use of function attributes.
They're still hidden when wrapped by an uncooperative decorator.
The decorator module may be helpful in defining cooperative decorators:
http://www.phyast.pitt.edu/~micheles...mentation.html

George

Sep 10 '06 #8
Thanks,
In Python 2.5 there are also functools.wraps and
functools.update_wrapper:
http://docs.python.org/dev/whatsnew/pep-309.html

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

The decorator module may be helpful in defining cooperative decorators:
http://www.phyast.pitt.edu/~micheles...mentation.html

George
Sep 10 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Philipp Lenssen | last post: by
4 posts views Thread by Elmo Mäntynen | last post: by
14 posts views Thread by Java and Swing | last post: by
1 post views Thread by Jonathan Gibbs | last post: by
2 posts views Thread by EP | last post: by
78 posts views Thread by Josiah Manson | last post: by
1 post views Thread by Thomas W | last post: by
4 posts views Thread by Tony Lownds | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.