Reputation: 88568
I've been trying to do the following:
#[...]
def __history_dependent_simulate(self, node, iterations=1,
*args, **kwargs):
"""
For history-dependent simulations only:
""" + self.simulate.__doc___
What I tried to accomplish here is to have the same documentation for this private method as the documentation of the method simulate
, except with a short introduction. This would allow me to avoid copy-pasting, keep a shorter file and not have to update the documentation for two functions every time.
But it doesn't work. Does anyone know of a reason why, or whether there is a solution?
Upvotes: 9
Views: 2076
Reputation: 399803
I think this section makes it pretty clear:
What is a Docstring?
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the doc special attribute of that object.
So, it's not an expression that evaluates into a string, it's a string literal.
Upvotes: 2
Reputation: 2914
A better solution is probably to use a decorator, eg:
def add_docs_for(other_func):
def dec(func):
func.__doc__ = other_func.__doc__ + "\n\n" + func.__doc__
return func
return dec
def foo():
"""documentation for foo"""
pass
@add_docs_for(foo)
def bar():
"""additional notes for bar"""
pass
help(bar) # --> "documentation for foo // additional notes for bar"
That way you can do arbitrary manipulations of docstrings.
Upvotes: 9