将文档字符串放在特殊方法上?
我正在尝试决定将哪些信息放入类文档字符串中以及将哪些信息放入 __init__ 方法文档字符串中。到目前为止,我一直在类文档字符串中放置类的概述以及如何使用它,而与初始化直接相关的内容(参数详细信息等)则放在 __init__
文档字符串中。
今天我开始想知道这是否是正确的方法,所以我查看了几个内置模块,我发现 __init__ 方法几乎从来没有文档字符串。根据 PEP8,“文档字符串对于非公共方法不是必需的”,但是 __init__
不是公共的吗?
同样,其他特殊方法,例如 __getitem__ 、 __getattr__ 或 __new__ ,它们应该有文档字符串吗?或者我应该在类文档字符串中提及它们的后果?
I'm trying to decide what information to put in the class docstring and what to put in the __init__
method docstring. Up until now I've been putting an overview of the class and how to work with it in the class docstring, while stuff directly related to initialization (argument details etc.) I put in the __init__
docstring.
Today I started wondering if this was the right way of doing it, so I looked at a couple of built-in modules, and I see that the __init__
method almost never has a docstring. According to PEP8, "Docstrings are not necessary for non-public methods", but isn't __init__
public?
In the same vein, what about other special methods, like __getitem__
, __getattr__
or __new__
, should they have docstrings? Or should I just mention the consequences they have in the class docstring?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接来自 PEP 257:
[...]
Straight from PEP 257:
[...]