cul*_*rón 2 python documentation decorator
在应用装饰器后,如何让函数中的帮助字符串保持可见?
现在,doc字符串被(部分)替换为装饰器的内部函数.
def deco(fn):
def x(*args, **kwargs):
return fn(*args, **kwargs)
x.func_doc = fn.func_doc
x.func_name = fn.func_name
return x
@deco
def y(a, b):
"""This is Y"""
pass
def z(c, d):
"""This is Z"""
pass
help(y) # 1
help(z) # 2
Run Code Online (Sandbox Code Playgroud)
在Y函数中,帮助中不显示必需的参数.用户可以假设它接受任何参数,而实际上它没有.
y(*args, **kwargs) <= y(a, b) is desired
This is Y
z(c, d)
This is Z
Run Code Online (Sandbox Code Playgroud)
我用help()和dir()了很多,因为它比PDF手册快,想为我的图书馆和工具,可靠的文档字符串,但这是一个障碍.
给装饰模块一个窥视.我相信它完全符合你的要求.
In [1]: from decorator import decorator
In [2]: @decorator
...: def say_hello(f, *args, **kwargs):
...: print "Hello!"
...: return f(*args, **kwargs)
...:
In [3]: @say_hello
...: def double(x):
...: return 2*x
...:
Run Code Online (Sandbox Code Playgroud)
并且信息中写着"double(x)".
| 归档时间: |
|
| 查看次数: |
129 次 |
| 最近记录: |