I'm trying to document a @property
in a python module and can't get the docstring to show up in the property's help. I would like a call to help(class.property)
to print the help for only that property.
Here's an example:
class ClassWithStringProperty(object):
def __init__(self):
self._str_obj = "'{0:02d}, {1}'.format(thingy['number'], thingy['description']"
@property
def str_obj(self):
"""
A configurable formatting string that is eval()'ed for data export
"""
return self._str_obj
@str_obj.setter
def str_obj(self, str_obj):
self._str_obj = str_obj
When I import and try to get help, it works on the whole class, but not on the individual property:
In [1]: from property_docstring import ClassWithStringProperty
In [2]: cwp = ClassWithStringProperty()
In [4]: help(cwp)
Help on ClassWithStringProperty in module property_docstring object:
class ClassWithStringProperty(__builtin__.object)
| Methods defined here:
|
| __init__(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| str_obj
| A configurable formatting string that is eval()'ed for data export
In [5]: help(cwp.str_obj)
no Python documentation found for "'{0:02d}, {1}'.format(thingy['number'], thingy['description']"
As you can see in the output from In [4]
, the docstring prints under str_obj
, but in In [5]
it says there's no Python documentation.
How can I allow access the @property's documentation on it's own without having to list the entire class's docs?
You are accessing the attribute on the instance, resulting in the getter being called and the result being passed to the help()
function. The value that the getter returned has no docstring.
Note that you are not actually using help(class.property)
here, you are using help(instance.property)
.
You'd need to look for help on the class instead; if you only have the instance, use type()
to give you the class:
help(type(cwr).str_obj)
or, if you already have the class, ask for help on the class:
help(ClassWithStringProperty.str_obj)
help(instance)
automatically detects you have an instance and gives you help on the class, but that can't be done for the results of properties, the relationship to the instance (and so to the class) is gone when help()
is called.