python - 在 python 文档字符串中编写默认值的标准方法是什么?

标签 python numpy docstring

我有一个参数设置为默认值的函数。我正在使用 NumPy-style docstring,但我在其他地方看到了默认值。在文档字符串中编写“默认”的普遍接受位置是什么?

def some_func(a_num=None, a_string=None):
    ''' A function that does something special.

    Parameters
    ==========
    a_num : int, default 100                        # is it written here?
        An important number.
    a_string : str, default 'foo'
        A useful string.  Default is 'foo'.         # or here?    
 
    '''

最佳答案

如果您在链接的文档中进一步阅读,它似乎没有一种标准样式:

Optional keyword parameters have default values, which are displayed as part of the function signature. They can also be detailed in the description:

Description of parameter `x` (the default is -1, which implies summation
over all axes).

When a parameter can only assume one of a fixed set of values, those values can be listed in braces, with the default appearing first:

order : {'C', 'F', 'A'}
    Description of `order`.

我建议为您自己的项目选择一种风格并坚持使用。

关于python - 在 python 文档字符串中编写默认值的标准方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34882861/

相关文章:

python - numpy ndarray 的解释

python - 在 matplotlib 中绘制 10 负幂的 xscale

python - sympy 矩阵真的那么慢吗?

python - 如何生成类似 numpy 的文档?

Python设置docstring并获取动态生成的类方法的方法名称

python - 使用 lambda 函数将 double for 循环转换为列表理解

python - 更新我的类和创建 Sprite 的方式后,移动的功能不再起作用

python - 如何在按下按键时随时间增加速度变量?

python - 在 Python 文档字符串中记录复杂嵌套字典参数的最佳实践?

python - 测试脚本输出