Inherit a parent class docstring as __doc__ attribute
Posted
by
Reinout van Rees
on Stack Overflow
See other posts from Stack Overflow
or by Reinout van Rees
Published on 2012-12-18T16:38:49Z
Indexed on
2012/12/18
17:03 UTC
Read the original article
Hit count: 416
There is a question about Inherit docstrings in Python class inheritance, but the answers there deal with method docstrings.
My question is how to inherit a docstring of a parent class as the __doc__
attribute. The usecase is that Django rest framework generates nice documentation in the html version of your API based on your view classes' docstrings. But when inheriting a base class (with a docstring) in a class without a docstring, the API doesn't show the docstring.
It might very well be that sphinx and other tools do the right thing and handle the docstring inheritance for me, but django rest framework looks at the (empty) .__doc__
attribute.
class ParentWithDocstring(object):
"""Parent docstring"""
pass
class SubClassWithoutDoctring(ParentWithDocstring):
pass
parent = ParentWithDocstring()
print parent.__doc__ # Prints "Parent docstring".
subclass = SubClassWithoutDoctring()
print subclass.__doc__ # Prints "None"
I've tried something like super(SubClassWithoutDocstring, self).__doc__
, but that also only got me a None
.
© Stack Overflow or respective owner