繁体   English   中英

如何打印类属性/元素的文档字符串?

[英]How to print docstring for class attribute/element?

我有一堂课:

class Holiday(ActivitiesBaseClass):
    """Holiday is an activity that involves taking time off work"""

    Hotel = models.CharField(max_length=255)
    """ Name of hotel """

我可以通过键入以下内容来打印类文档字符串:

print(Holiday.__doc__)

这输出为:

Holiday is an activity that involves taking time off work 

如何打印属于 Class 属性/元素的 Hotel 的文档字符串?

我试过的

print(Holiday.Hotel.__doc__)

返回:

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

我也试过help()函数无济于事。

NB

Sphinx 似乎能够提取类属性的文档字符串并将它们包含在 readthedocs 中,所以我希望有一种方法

不要认为可以为特定字段添加文档字符串,但您可以使用字段的help_text参数:

Hotel = models.CharField(max_length=255, help_text="Name of hotel")

不幸的是,目前在 Python 中没有这样的属性文档字符串实现。 有一个PEP建议实现此功能,但被拒绝。

暂无
暂无

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

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