如何在Python中记录类属性?

如何在Python中记录类属性?,第1张

如何在Python中记录类属性?

为避免混淆:术语 property
在python中具有特定含义。您所说的是所谓的
类属性
。由于始终在类中对它们进行 *** 作,因此我发现将它们记录在类的文档字符串中是有意义的。像这样:

class Albatross(object):    """A bird with a flight speed exceeding that of an unladen swallow.    Attributes:        flight_speed     The maximum speed that such a bird can attain.        nesting_grounds  The locale where these birds congregate to reproduce.    """    flight_speed = 691    nesting_grounds = "Throatwarbler Man Grove"

我认为这比示例中的方法容易得多。如果我确实希望属性值的副本出现在doc字符串中,则可以将它们放在每个属性的描述的旁边或下方。

请记住,在Python中,文档字符串是其文档对象的实际成员,而不仅仅是源代码注释。由于类属性变量本身不是对象而是对象的引用,因此它们无法保存自己的文档字符串。我想您可以为引用中的doc字符串辩护,也许可以描述“应该在这里做什么”而不是“实际在这里”,但是我发现在包含类的doc字符串中这样做很容易。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5647235.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存