RACAD stands for Runtime Access for Class Attribute Docstrings. This is the source code accompanying my blogpost.
You can copy this code into your own project or use it as a library.
Given a class defined as follows:
class MyClass:
a: int = 5
"""This is the docstring of a."""
to get the attribute docstring of one attribute of a class by its name, use:
from racad import get_attribute_docstring
get_attribute_docstring(MyClass, 'a')
# Output: 'This is the docstring of a.'
alternatively, to get all docstrings of all attributes of a class, use:
from racad import get_attribute_docstrings
get_attribute_docstrings(MyClass)
# Output: {'a': 'This is the docstring of a.'}
If you want to also search the base classes, set search_bases=True
:
class MyChild(MyClass):
b: str = "Hi"
"""This is the docstring for b."""
get_attribute_docstrings(MyChild, search_bases=True)
# Output: {'a': 'This is the docstring of a.', 'b': 'This is the docstring for b.'}
This also works for get_attribute_docstring
.
Requires source code access via the inspect
module.
E.g. does not work if the class has been defined in a REPL.