Skip to content Skip to sidebar Skip to footer

Reuse Docstring From A Method In A Class In Python

So I have a method in a class and I have another separate function (i.e., outside the class) that want to reuse the docstring of that method. I tried something like __doc__ =

Solution 1:

__doc__ needs to be assigned as a property of the new function, like this:

classC:
    deffoo(self):
        'docstring'defbar():
    pass

bar.__doc__ = C.foo.__doc__  # NOT __doc__ = ...assert bar.__doc__ == 'docstring'

Solution 2:

Even this is a case, I'd use a manual copy of a docstring. Class or function could be moved around or separated to different projects. Moreso, reading a function below goesn't give me any idea what it's doing.

Please, consult with PEP-8, PEP-257 and PEP-20 for more information why this behavior is discoraged.

def myfunc():
    ...


myfunc.__doc__ = other_obj.__doc__

Post a Comment for "Reuse Docstring From A Method In A Class In Python"