doctest中的这段代码在单独运行时起作用,但在这个doctest中,它在10个地方失败了.我无法弄清楚它为什么会这样.以下是整个模块:
class requireparams(object):
"""
>>> @requireparams(['name', 'pass', 'code'])
>>> def complex_function(params):
>>> print(params['name'])
>>> print(params['pass'])
>>> print(params['code'])
>>>
>>> params = {
>>> 'name': 'John Doe',
>>> 'pass': 'OpenSesame',
>>> #'code': '1134',
>>> }
>>>
>>> complex_function(params)
Traceback (most recent call last):
...
ValueError: Missing from "params" argument: code
"""
def __init__(self, required):
self.required = set(required)
def __call__(self, params):
def wrapper(params):
missing = self.required.difference(params)
if missing:
raise ValueError('Missing from "params" argument: %s' % ', '.join(sorted(missing)))
return wrapper
if __name__ == …Run Code Online (Sandbox Code Playgroud) 我有一个sphinx格式的docstring,我想从中提取不同的部分(param,return,type,rtype等)以供进一步处理.我怎样才能做到这一点?
我正在观看关于Udacity的CS262的视频,我对这种程序定义感到困惑:
def t_WORD(token):
r'[^ <>]+' # any reg. exp. ruleset is placed here
# ... more processing
# ... more processing
return token
Run Code Online (Sandbox Code Playgroud)
此代码使用库层(.lex)
我有一些Python的经验和知识,但我对此过程定义行之后的第一行非常困惑.
Python解释器如何使用,解释或访问该字符串(reg.exp.string)?它只是一个没有指向它的变量而没有分配的字符串.
我已经完成了通常的Google和SO搜索,但无法找到它实际上是什么.
提前感谢您的所有答案和解释.
所以我正在打字
>>> list = [1,2,3]
>>> list.__doc__
"list() -> new empty list\nlist(iterable) -> new list initialized from iterable's items"
Run Code Online (Sandbox Code Playgroud)
问题在于它字面上打印"\n"而不是打印新行,使得更复杂的文档字符串几乎无法读取.
我尝试过的所有其他内置类都会发生同样的事情.
我打开它时从python获得的版本信息是:darwin上的Python 2.7.5(默认,2013年7月16日,14:23:29)[GCC 4.2.1兼容的Apple LLVM 4.2(clang-425.0.28)]
我的代码看起来像这样:
constString = """
Default docstring info:
1
2
3"""
class A():
def A1():
"""
First unique docstring.
"""
pass
def A2():
"""
Second unique docstring.
"""
pass
B = A()
print(B.A1.__doc__)
Run Code Online (Sandbox Code Playgroud)
如果我运行此代码,我会重新输出:
First unique docstring.
Second unique docstring.
Run Code Online (Sandbox Code Playgroud)
但是我想通过为类A中的所有方法添加constString来替换方法的docstring.输出必须如下所示:
First unique docstring.
Default docstring info:
1
2
3
Second unique docstring.
Default docstring info:
1
2
3
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做?
我有一个像这样开始的函数:
def apply_weighting(self, weighting):
"""
Available functions: {}
""".format(weightings)
Run Code Online (Sandbox Code Playgroud)
我想要的是docstring打印可用加权函数的字典.但是在检查函数时,它声明没有可用的文档字符串:
In [69]: d.apply_weighting?
Type: instancemethod
String Form:<bound method DissectSpace.apply_weighting of <dissect.DissectSpace instance at 0x106b74dd0>>
File: [...]/dissect.py
Definition: d.apply_weighting(self, weighting)
Docstring: <no docstring>
Run Code Online (Sandbox Code Playgroud)
怎么会?是否无法格式化文档字符串?
我在其他人的Python代码中看到他们在函数的docstring之前粘贴了一些检查; 这样的事情:
def func(args):
if x_version != 1.7:
return
"""docstring is here"""
# function body code
# ...
Run Code Online (Sandbox Code Playgroud)
是否有当您在任何情况下,应该或必须把一些代码的文档字符串之前要解决什么?是否有任何特殊情况需要引入这种风格,或者它总是只是一个糟糕的造型,因此必须修复到这样的东西?
def func(args):
"""docstring is here"""
if x_version != 1.7:
return
# function body code
# ...
Run Code Online (Sandbox Code Playgroud) 我有几个函数(a,b和c),我希望它们使用相同的文档字符串。所以我的计划是通过只写一次文档字符串并将其保存到变量来保存行DOCSTRING。然后我把它放在函数声明下。我在PEP 257中没有找到任何可以解决我的问题的内容......
DOCSTRING = '''
This is a docstring
for functions:
a,
b,
c'''
def a(x, y):
DOCSTRING
# do stuff with x and y
def b(x, y):
DOCSTRING
# do other stuffs with x and y
def c(x, y):
DOCSTRING
# do some more stuffs with x and y
help(a), help(b), help(c)
Run Code Online (Sandbox Code Playgroud)
我实际上认为它可能有效......但我错了,我得到了这个:
Help on function a in module __main__:
a(x, y)
Help on function b in module __main__: …Run Code Online (Sandbox Code Playgroud) 我通常使用带有""的多行文档字符串来评论我的函数,如下所述:https: //www.python.org/dev/peps/pep-0257/
def func1(x):
"""
This function does ...
"""
...
Run Code Online (Sandbox Code Playgroud)
但是评论lambda函数的最佳方法是什么?我在犹豫之间犹豫:
# This function does ...
func2 = lambda x: ...
Run Code Online (Sandbox Code Playgroud)
要么 :
func2 = lambda x: ...
""" This function does ... """
Run Code Online (Sandbox Code Playgroud)
要不然 ?
似乎有两个地方可以为一个类放置docstrings:
class MyClass(object):
""" Summary of MyClass
Body
...
"""
Run Code Online (Sandbox Code Playgroud)
__init__构造函数下面:...
def __init__(self, arg1, arg2):
""" Summary of MyClass
Body
...
"""
Run Code Online (Sandbox Code Playgroud)
哪个更受欢迎?或者两个都可以吗?