9

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?

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Arbitrary manipulation means you could do have "SEE DOCS FOR >foo<" in the docstring, and have the decorator use re to replace that with foo.__doc__, eg.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.