gpt4 book ai didi

python - 类中的文档字符串或 __init__ 构造函数?

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:23 25 4
gpt4 key购买 nike

似乎有两个地方可以放置类的文档字符串:

  1. 右下类定义:

    class MyClass:
    """Summary of MyClass.

    Body.
    """
  2. __init__ 构造函数下:

    class MyClass:
    def __init__(self, arg1, arg2):
    """Summary of MyClass.

    Body.
    """

哪个是首选?还是两者都可以?

最佳答案

它们可以同时存在,因为它们的目的不同。

The docstring for a class should summarize its behavior and list the public methods and instance variables. If the class is intended to be subclassed, and has an additional interface for subclasses, this interface should be listed separately (in the docstring). The class constructor should be documented in the docstring for its __init__ method. Individual methods should be documented by their own docstring.

强调我的。此引用来自 PEP 257 -- Docstring Conventions

关于python - 类中的文档字符串或 __init__ 构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com