Meta decorator with parameters, defined in explicit functions
Lawrence D’Oliveiro
lawrencedo99 at gmail.com
Sun Jul 3 19:59:26 EDT 2016
On Monday, July 4, 2016 at 11:39:56 AM UTC+12, Chris Angelico wrote:
> In the same way, people expect "f = deco(f)" to return *the same function,
> decorated*.
I am not changing that in anyway. The decorated function generated by the caller is returned *unchanged*.
Once again, the only docstrings I am setting are for the intermediate functions *I* generate.
Suppose we have the following:
def try_func(func, arg) :
"sample function to be turned into a decorator."
def result() :
"returns func unchanged."
return \
func()
#end result
#begin try_func
result.__name__ = func.__name__
result.__doc__ = func.__doc__
return \
result
#end try_func
then:
>>> f = decorator_with_args(try_func)
>>> help(f)
Help on function decorate_with_try_func in module decorator_try:
decorate_with_try_func(*args, **kwargs)
generates a decorator which applies try_func to the given arguments
>>> @f(None)
... def g() :
... "my docstring--should be unchanged."
... return \
... 1
... #end g
...
>>> help(g)
Help on function g in module decorator_try:
g()
my docstring--should be unchanged.
Do you understand now?
More information about the Python-list
mailing list