PEP 257说:
在记录一个类的所有docstring (一行或多行)之前和之后插入一个空行--一般来说,类的方法由一个空行分隔开来,而docstring需要用空行与第一个方法相抵消;为了对称,在类标头和docstring之间放置一个空行。
但我似乎找不到任何真正实现这一点的代码。
我检查了Python2.6提供的几个标准模块,甚至专门搜索提到Guido名称的模块。但是,即使是rietveld代码审查工具的代码也不符合IMHO的要求(例如,请参见http://code.google.com/p/rietveld/source/browse/upload.py):
class CondensedHelpFormatter(optparse.IndentedHelpFormatter):
"""Frees more horizontal space by removing indentation from group
options and collapsing arguments between short and long, e.g.
'-o ARG, --opt=ARG' to -o --opt ARG"""
def format_heading(self, heading):
return "%s:\n" % heading
此多行docstring在“前”中没有空行,“后”则位于“结束引号”之外。
来自/usr/lib64/python2.6/site.py
的该类在结束引号之前和之后没有空行,而是有空行。
class _Helper(object):
"""Define the built-in 'help'.
This is a wrapper around pydoc.help (with a twist).
"""
def __repr__(self):
是否有演示PEP 257的例子?
提前感谢
发布于 2020-06-16 11:51:30
下面是一些pep() python示例示例,我们首先选择要使用的版本,比如这个示例与pep-8非常相似。所以我们必须提供函数描述,parm和返回类型..。
def foo(bar, spam, eggs):
"""
Some function
:param bar: parameter that requires description
:param spam: parameter that requires description
:param eggs:
:return xyz: parameter description
"""
根据谷歌的风格,包含了一个很好的蟒蛇样式指南。这提供了比PEP 257更好的指导,下面是参考链接:谷歌风格指南
def sample_fun(n):
"""Calculate the square root of a number.
Args:
n: the number to get the square root of.
Returns:
the square root of n.
Raises:
TypeError: if n is not a number.
ValueError: if n is negative.
"""
pass
https://stackoverflow.com/questions/10017776
复制相似问题