简体   繁体   English

有没有一种用`python-attrs`记录属性的好方法?

[英]Is there a good way to document attributes with `python-attrs`?

I'm used to documenting my __init__() function with a docstring, but I would like to take advantage of the benefits of the attrs package.我习惯于用文档字符串记录我的__init__()函数,但我想利用attrs包的好处。 The docstring is useful in IPython or a Jupyter notebook, so I can see what the parameters mean.文档字符串在 IPython 或 Jupyter 笔记本中很有用,因此我可以看到参数的含义。 Is there a good way to do this?有没有好的方法可以做到这一点? In this example code:在此示例代码中:

@atrr.s
class Coordinates(object):
    """ A set of coordinates
    """
    x = attr.ib()
    y = attr.ib()

"""
In [1]: Coordinates?
Init signature: Coordinates(x, y) -> None
Docstring:     
A set of coordinates
    
Init docstring: Method generated by attrs for class Coordinates.
Type:           type
Subclasses:
"""

how can I describe the x and y variables to the user?如何向用户描述xy变量? For example, how can I specify that these are in degrees?例如,我如何指定这些以度为单位?

In Python, the documentation of attributes and (more importantly) __init__ arguments happens in the class docstring , therefore the presence of attrs is not important in this case:在 Python 中,属性和(更重要的) __init__参数的文档发生在类 docstring 中,因此在这种情况下attrs的存在并不重要:

@attr.define
class Coordinates:
    """
    A set of coordinates.

    :param int x: Foo in degrees.
    :param int y: Bar in degrees.
    """
    x: int
    y: int

For more information check out RTD's docs on writing docstrings .有关更多信息,请查看 RTD 关于编写 docstrings 的文档

If you don't like this format, another common one is called Napoleon and comes from Google: https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html如果你不喜欢这种格式,另一种常见的叫做 Napoleon,来自谷歌: https : //www.sphinx-doc.org/en/master/usage/extensions/napoleon.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM