Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print docstring for class attribute/element?

I have a class:

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

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

I can print the class docstring by typing:

print(Holiday.__doc__)

This outputs as:

Holiday is an activity that involves taking time off work 

How can I print the docstring for Hotel which is a Class attribute/element?

What I've tried

print(Holiday.Hotel.__doc__)

returns:

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

I've also tried the help() function to no avail.

N.B

Sphinx seems to be able to extract the docstrings of class attributes and include them in readthedocs so I’m hoping there’s a way

like image 662
Toms Code Avatar asked Jan 23 '20 14:01

Toms Code


2 Answers

Don't think it's possible to add docstring for specific field, but you can use field's help_text argument instead:

Hotel = models.CharField(max_length=255, help_text="Name of hotel")
like image 184
neverwalkaloner Avatar answered Sep 18 '22 14:09

neverwalkaloner


Unfortunately, there is currently no such implementation of attribute docstrings in Python. There was a PEP that suggested implementing this functionality, but it was rejected.

like image 31
CDJB Avatar answered Sep 18 '22 14:09

CDJB