gpt4 book ai didi

类与类对象实例的 Python 文档样式

转载 作者:行者123 更新时间:2023-12-05 07:46:14 24 4
gpt4 key购买 nike

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

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 标准是使用重组文本的 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`

或者使用拿破仑:

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

关于类与类对象实例的 Python 文档样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40950257/

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