[BangPypers] Expressing meta-data about classes, methods, variables, parameters in python

Sidharth Kuruvila sidharth.kuruvila at gmail.com
Thu Apr 3 15:15:59 CEST 2008


Hi,

You can simulate annotation using decorators. I might be missing  
something about annotation but you can do this.

def annotate(returns, **params):
     def decorate(func):
         params['returns'] = returns
         func.__annotations__ = params
         return func
     return decorate

@annotate("returns", a="a")
def f(a):
     return a

print f.__annotations__




On Apr 3, 2008, at 4:34 PM, Heshan Suriyaarachchi wrote:

> Hi Anand,
>    This is what I exactly mean. This is the thing I want to do. I am  
> now using python 2.5.1 . So is
>    there a way that I can do a thing like this in python 2.5.1 ( an  
> alternative way).
>
>   Heshan Suriyaarachchi
>
>
> >>> def f(x : "an integer", y: "another integer") -> "Sum of x and y":
> ...     return x + y
> >>> f.__annotations__
> {'y': 'another integer', 'x': 'an integer', 'return': 'Sum of x and  
> y'}
>
> The help() function returns the annotation plus the docstring now.
> >>> f.__doc__ = "A sum function"
> >>> help(f)
> Help on function f in module __main__:
>
> f(x: 'an integer', y: 'another integer') -> 'Sum of x and y'
>    A sum function
>
>
>
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers



More information about the BangPypers mailing list