Suppose I have a class like this:
class TestCase(object):
"""Class docstring"""
def meth(self):
"""Method docstring"""
return 1
@property
def prop(self):
"""Property docstring"""
return 2
It's easy enough for me to get the docstrings for the class itself, or for a regular method:
tc = TestCase()
print(tc.__doc__)
# Class docstring
print(tc.meth.__doc__)
# Method docstring
However, this approach doesn't work for properties - instead I get the __doc__
attribute of whatever object is returned by the property getter method (in this case, int
):
print(tc.prop.__doc__)
# int(x=0) -> int or long
# int(x, base=10) -> int or long
# ...
The same thing applies to getattr(tc, "prop").__doc__
and getattr(tc.prop, "__doc__")
.
I know that Python's introspection mechanisms are capable of accessing the docstring I'm looking for. For example, when I call help(tc)
I get:
class TestCase(__builtin__.object)
| Class docstring
|
| Methods defined here:
|
| meth(self)
| Method docstring
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| prop
| Property docstring
How is help
able to access the docstring for tc.prop
?