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

时间:2020-01-23 14:35:25

标签: python django python-3.x django-models docstring

我有课:

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()函数,但无济于事。

N.B

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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:4)

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

相关问题