【问题标题】:Python documentation style for instances of class vs class objects类与类对象实例的 Python 文档样式
【发布时间】:2016-12-03 17:04:28
【问题描述】:

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

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

【问题讨论】:

    标签: python documentation docstring


    【解决方案1】:

    python 标准是使用 restructuredtext (http://www.sphinx-doc.org/en/1.4.9/rest.html) 的 sphinx 样式

    更准确地说,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`
    

    或者使用拿破仑:

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

    【讨论】:

    • 正确的语法是 :py:class: 但这并不能回答问题,因为 OP 正在寻找“rtype”样式语法(即类型提示)
    • 他也不是在寻找rtype。他正在寻找类“类型”而不是实例类型的 :py:class 等价物。
    • 现在这个答案404中的所有链接。
    猜你喜欢
    • 2015-12-16
    • 1970-01-01
    • 2012-05-05
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多