I am making a large number (close to 100) of classes programmatically and I would like to be able to set the docstring of the class. Each of the class docstrings will have the same basic structure, with specific pieces to be filled in. I would like to define a template string and then populate it with class specific arguments when the class is created.
As a contrived example of what I am after, consider the following
class_docstr = """
This is the docstring for the {name} class.
"""
class A(object):
def __init__(self):
pass
class B(object):
def __init__(self):
pass
class C(object):
def __init__(self):
pass
What I would like to do is somehow set A.__doc__ = class_docstr.format(name="A")
so that when I call help(A)
I see something like this:
class A(__builtin__.object)
| This is the docstring for the A class.
I believe I might need to use a metaclass to do this, but I am not sure how.
You can set the docstring by assigning to the __doc__
class variable inside the class definition:
class A(object):
__doc__ = class_docstr.format(name="A")
# whatever else
This works, even though assigning to A.__doc__
does not work later, after the class has been created.