Python文档风格:类的实例与类对象的区别

3

如何标准地记录函数参数和类属性的类型,以区分那些预期是类实例和那些预期是类对象本身?

class TestClass(object):
    def __init__(self):
        pass

class Objectify(object):
    def __init__(self):
        pass    

class NeatDocumentation(object):
    """A class demonstrating neat documentation style.

    Attributes:
        cls (TestClass?): A class you want to test.
        obj (Objectify?): An instance of `Objectify` class.
        string (str): A string because why not.
    """

    def __init__(self, cls_, obj, string):
        self.cls = cls_  # An instance can be created by executing self.cls()
        self.obj = obj
        self.string = string
1个回答

0

Python的标准是使用reStructuredText的Sphinx风格(http://www.sphinx-doc.org/en/1.4.9/rest.html)。

更确切地说,是autodoc模块风格:http://www.sphinx-doc.org/en/1.4.8/ext/autodoc.html

如果你想要更漂亮的字符串,也可以使用sphinx-napoleon风格: http://www.sphinx-doc.org/en/1.4.9/ext/napoleon.html

在你的情况下,你可以这样做:

:param your_param: Class to test
:type your_param: :class:`Objectify`

或者使用 napoleon:

Args:
    your_param (Objectify): The :class:`Objectify` class to test

1
正确的语法是 :py:class:,但这并不能回答问题,因为 OP 正在寻找“rtype”风格的语法(即类型提示)。 - Thiago Figueiro
他也不是在寻找 rtype。他正在寻找类 'type' 的等效项 :py:class,而不是实例类型。 - stelios
这个答案中的所有链接现在都是404。 - Todd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接