@func call syntax

Schüle Daniel uval at rz.uni-karlsruhe.de
Sun Jun 11 20:52:38 EDT 2006


this is decorator, this is how it's may be implented

 >>> def returns(t):
...     def dec(f):
...             def wrapped(*args, **kwargs):
...                     ret = f(*args, **kwargs)
...                     assert type(ret) is t
...                     return ret
...             return wrapped
...     return dec
...
 >>>
 >>> @returns(int)
... def f1():
...     return 1
...
 >>> @returns(float)
... def f2():
...     return 2.0
...
 >>> @returns(str)
... def f3():
...     return 1
...
 >>> f1()
1
 >>> f2()
2.0
 >>> f3()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 5, in wrapped
AssertionError
 >>>


I can imagine that stuff like this may be extremely usefull
when testing you program
later one could parse and remove all such assertations
easy and cut them all at once

Regards, Daniel



More information about the Python-list mailing list