views:

85

answers:

2

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?

+5  A: 

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.

Anthony Towns
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.
Anthony Towns
+1  A: 

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.

unwind