Function metadata (like Java annotations) in Python

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sat Sep 9 23:11:32 EDT 2006


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




More information about the Python-list mailing list