如何在 Python 中记录类属性?
我正在编写一个轻量级类,其属性旨在可公开访问,并且仅有时在特定实例化中被覆盖。 Python 语言中没有为类属性或任何类型的属性创建文档字符串的规定。记录这些属性的预期和支持的方式是什么(是否应该有一种方式)?目前我正在做这样的事情:
class Albatross(object):
"""A bird with a flight speed exceeding that of an unladen swallow.
Attributes:
"""
flight_speed = 691
__doc__ += """
flight_speed (691)
The maximum speed that such a bird can attain.
"""
nesting_grounds = "Raymond Luxury-Yacht"
__doc__ += """
nesting_grounds ("Raymond Luxury-Yacht")
The locale where these birds congregate to reproduce.
"""
def __init__(self, **keyargs):
"""Initialize the Albatross from the keyword arguments."""
self.__dict__.update(keyargs)
这将导致类的文档字符串包含初始标准文档字符串部分,以及通过对 __doc__
的增强赋值为每个属性添加的行。
尽管文档字符串样式指南中似乎没有明确禁止这种样式,它也没有被提及作为一个选项。这里的优点是它提供了一种记录属性及其定义的方法,同时仍然创建可呈现的类文档字符串,并避免编写注释来重申文档字符串中的信息。我仍然有点恼火,因为我实际上必须将属性写两次;我正在考虑使用文档字符串中值的字符串表示形式,以至少避免默认值的重复。
这是否严重违反了临时社区公约?可以吗?有更好的办法吗?例如,可以创建一个包含属性值和文档字符串的字典,然后将内容添加到类 __dict__
中,并将文档字符串添加到类声明的末尾;这将减少两次键入属性名称和值的需要。 编辑:我认为,最后一个想法实际上是不可能的,至少在没有从数据动态构建整个类的情况下是不可能的,这似乎是一个非常糟糕的主意,除非有其他原因这样做。
我对 python 还很陌生,仍在研究编码风格的细节,所以也欢迎不相关的批评。
I'm writing a lightweight class whose attributes are intended to be publicly accessible, and only sometimes overridden in specific instantiations. There's no provision in the Python language for creating docstrings for class attributes, or any sort of attributes, for that matter. What is the expected and supported way, should there be one, to document these attributes? Currently I'm doing this sort of thing:
class Albatross(object):
"""A bird with a flight speed exceeding that of an unladen swallow.
Attributes:
"""
flight_speed = 691
__doc__ += """
flight_speed (691)
The maximum speed that such a bird can attain.
"""
nesting_grounds = "Raymond Luxury-Yacht"
__doc__ += """
nesting_grounds ("Raymond Luxury-Yacht")
The locale where these birds congregate to reproduce.
"""
def __init__(self, **keyargs):
"""Initialize the Albatross from the keyword arguments."""
self.__dict__.update(keyargs)
This will result in the class's docstring containing the initial standard docstring section, as well as the lines added for each attribute via augmented assignment to __doc__
.
Although this style doesn't seem to be expressly forbidden in the docstring style guidelines, it's also not mentioned as an option. The advantage here is that it provides a way to document attributes alongside their definitions, while still creating a presentable class docstring, and avoiding having to write comments that reiterate the information from the docstring. I'm still kind of annoyed that I have to actually write the attributes twice; I'm considering using the string representations of the values in the docstring to at least avoid duplication of the default values.
Is this a heinous breach of the ad hoc community conventions? Is it okay? Is there a better way? For example, it's possible to create a dictionary containing values and docstrings for the attributes and then add the contents to the class __dict__
and docstring towards the end of the class declaration; this would alleviate the need to type the attribute names and values twice. edit: this last idea is, I think, not actually possible, at least not without dynamically building the entire class from data, which seems like a really bad idea unless there's some other reason to do that.
I'm pretty new to python and still working out the details of coding style, so unrelated critiques are also welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简而言之:类属性不能像类和函数那样具有文档字符串。
为了避免混淆,术语属性具有特定含义 在Python中。您所说的就是我们所说的类属性 。由于它们总是通过类来执行,因此我发现在类的文档字符串中记录它们是有意义的。像这样的事情:
我认为这比你的例子中的方法更容易看。如果我确实希望属性值的副本出现在文档字符串中,我会将它们放在每个属性的描述旁边或下方。
请记住,在 Python 中,文档字符串是它们所记录的对象的实际成员,而不仅仅是源代码注释。由于类属性变量本身不是对象而是对象的引用,因此它们无法保存自己的文档字符串。我想您可以为引用上的文档字符串提供一个案例,也许是为了描述“这里应该放什么”而不是“这里实际上是什么”,但我发现在包含类文档字符串中做到这一点很容易。
In short: class attributes cannot have doc strings in the way that classes and functions have.
To avoid confusion, the term property has a specific meaning in python. What you're talking about is what we call class attributes. Since they are always acted upon through their class, I find that it makes sense to document them within the class' doc string. Something like this:
I think that's a lot easier on the eyes than the approach in your example. If I really wanted a copy of the attribute values to appear in the doc string, I would put them beside or below the description of each attribute.
Keep in mind that in Python, doc strings are actual members of the objects they document, not merely source code annotations. Since class attribute variables are not objects themselves but references to objects, they have no way of holding doc strings of their own. I guess you could make a case for doc strings on references, perhaps to describe "what should go here" instead of "what is actually here", but I find it easy enough to do that in the containing class doc string.
其他答案非常已经过时了。 PEP-257 描述了如何使用属性的文档字符串。奇怪的是,它们出现在属性之后:
它也适用于这样的类型注释:
VSCode 支持显示这些。
The other answers are very outdated. PEP-257 describes how you can use docstrings for attributes. They come after the attribute, weirdly:
It also works for type annotations like this:
VSCode supports showing these.
您在 What 部分引用了 PEP257:文档字符串约定是一个文档字符串,它指出:
PEP 258:属性文档字符串部分对此进行了更详细的解释。
如上所述,
属性不是可以拥有
__doc__
的对象,因此它们不会出现在help()
或 pydoc 中。这些文档字符串只能用于生成的文档。它们在 Sphinx 中与 指令自动属性。
Sphinx 可以在赋值之前的行上使用注释,或者在赋值之后使用特殊注释,或者在定义之后使用文档字符串,这些注释将被自动记录。
You cite the PEP257: Docstring Conventions, in the section What is a docstring it is stated:
And this is explained in more details in the PEP 258: Attribute Docstrings section.
As explains above,
an attribute is not an object that can own a
__doc__
so they won't appear inhelp()
or pydoc. These docstrings can only be used for generated documentation.They are used in Sphinx with the directive autoattribute.
Sphinx can use comments on a line before an assignment or a special comment following an assignment or a docstring after the definition which will be autodocumented.
您可以滥用属性来达到此目的。属性包含一个 getter、一个 setter、一个删除器、和一个文档字符串。天真地,这会变得非常冗长:
然后你将拥有一个属于 Cx 的文档字符串:
对许多属性执行此操作很麻烦,但你可以设想一个辅助函数 myprop:
然后,调用 Python 交互式
help
将给予:我认为这应该正是你所追求的。
编辑:我现在意识到,我们也许可以完全避免将第一个参数传递给
myprop
,因为内部名称并不重要。如果myprop
的后续调用可以以某种方式相互通信,它可以自动决定一个很长且不太可能的内部属性名称。我确信有一些方法可以实现这一点,但我不确定它们是否值得。You could abuse properties to this effect. Properties contain a getter, a setter, a deleter, and a docstring. Naively, this would get very verbose:
Then you will have a docstring belonging to C.x:
To do this for many attributes is cumbersome, but you could envision a helper function myprop:
Then, calling Pythons interactive
help
will give:which I think should be pretty much what you're after.
Edit: I realise now that we can perhaps avoid to need to pass the first argument to
myprop
at all, because the internal name doesn't matter. If subsequent calls ofmyprop
can somehow communicate with each other, it could automatically decide upon a long and unlikely internal attribute name. I'm sure there are ways to implement this, but I'm not sure if they're worth it.这是滥用
ast
和inspect
的答案。除了更改文档字符串之外,它对原始类实现没有任何作用。想法
实现
示例
help()
的输出:Here's an answer that abuses
ast
andinspect
. It does nothing to the original class implementation besides changing the docstring.The idea
The implementation
The example
The output of
help()
: