python - 为什么要在 Python 中设置部分对象的 __doc__?

标签 python functools

help() doesn't show the __doc__的部分对象。 然而,docs 中的示例设置它:

>>> from functools import partial
>>> basetwo = partial(int, base=2)
>>> basetwo.__doc__ = 'Convert base 2 string to an int.'
>>> basetwo('10010')
18

为什么设置 __doc__,如果它没有帮助?

最佳答案

为部分函数设置 __doc__ 与为更大的函数设置相同:

def somefunc(somerandomarg):
    """This is a docstring
    What I am doing is shorthand for __doc__ = "random docstring"
    For partials, you can't do this, so you set it with __doc__"""
    pass

记录任何东西总是好的,即使是部分的。

关于python - 为什么要在 Python 中设置部分对象的 __doc__?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125211/

相关文章:

python - 属性错误 : 'Dimension' object has no attribute 'log10' while using Keras Sequential Model. 适合

python - super 在子类化 tuple() 时给出错误的类型

python - 递归 Python 快速排序中的变量作用域

python - 在 I/O 跟踪文件中查找缓存未命中、命中率

python - 一种从字典 python 中查找嵌套值的方法

python - 如何将 functools.partial 与 os.path.join 一起使用?

python - 让 Python 脚本作为 TCP 套接字服务器在 AWS 计算机上运行

python - 如何将 imdb 列表文件导入 postgresql 数据库?

python - 从函数内部禁用 `functools.lru_cache`

python - '如果' vs. `while` 到 `functools.partial.func`