如何使用 pipetools 为以函数范式编写的函数编写文档字符串
How to write docstrings for functions written in functional paradigm using pipetools
我使用 pipetools 编写函数,它是“python 的函数管道库”。
一个示例函数(来自他们的docs):
pyfiles_by_length = (pipe
| os.listdir
| where(X.endswith('.py'))
| sort_by(len).descending
| (enumerate, X, 1)
| foreach("{0}. {1}")
| '\n'.join)
这是一个函数定义。我们如何为这样的函数编写文档字符串
我尝试过的:
- 函数前注释(优点:简洁明了。缺点:不适用于文档库)
# print python filenames in descending order by length
pyfiles_by_length = (pipe
| os.listdir
| where(X.endswith('.py'))
| sort_by(len).descending
| (enumerate, X, 1)
| foreach("{0}. {1}")
| '\n'.join)
__doc__
(优点:适用于文档库,缺点:不够简洁)
pyfiles_by_length = (pipe
| os.listdir
| where(X.endswith('.py'))
| sort_by(len).descending
| (enumerate, X, 1)
| foreach("{0}. {1}")
| '\n'.join)
pyfiles_by_length.__doc__ = 'print python filenames in descending order by length'
如果您需要文档字符串,我建议您使用标准 Python 方式:
def pyfiles_by_length(directory):
"""
Returns all Python files in `directory` sorted by length
"""
return (directory > pipe
| os.listdir
| where(X.endswith('.py'))
| sort_by(len).descending
| (enumerate, X, 1)
| foreach("{0}. {1}")
| '\n'.join)
我使用 pipetools 编写函数,它是“python 的函数管道库”。
一个示例函数(来自他们的docs):
pyfiles_by_length = (pipe
| os.listdir
| where(X.endswith('.py'))
| sort_by(len).descending
| (enumerate, X, 1)
| foreach("{0}. {1}")
| '\n'.join)
这是一个函数定义。我们如何为这样的函数编写文档字符串
我尝试过的:
- 函数前注释(优点:简洁明了。缺点:不适用于文档库)
# print python filenames in descending order by length
pyfiles_by_length = (pipe
| os.listdir
| where(X.endswith('.py'))
| sort_by(len).descending
| (enumerate, X, 1)
| foreach("{0}. {1}")
| '\n'.join)
__doc__
(优点:适用于文档库,缺点:不够简洁)
pyfiles_by_length = (pipe
| os.listdir
| where(X.endswith('.py'))
| sort_by(len).descending
| (enumerate, X, 1)
| foreach("{0}. {1}")
| '\n'.join)
pyfiles_by_length.__doc__ = 'print python filenames in descending order by length'
如果您需要文档字符串,我建议您使用标准 Python 方式:
def pyfiles_by_length(directory):
"""
Returns all Python files in `directory` sorted by length
"""
return (directory > pipe
| os.listdir
| where(X.endswith('.py'))
| sort_by(len).descending
| (enumerate, X, 1)
| foreach("{0}. {1}")
| '\n'.join)