Search code examples
pythondocumentationdocstring

How to add constant string to __doc__ for all class methods?


I have code that looks like this:

constString = """
Default docstring info:
    1
    2
    3"""

class A():

    def A1():
        """
        First unique docstring.
        """
        pass

    def A2():
        """
        Second unique docstring.
        """
        pass
B = A()
print(B.A1.__doc__)

If I run this code I recive output:

First unique docstring.

Second unique docstring.

But I want to replace method's docstring by adding constString for all methods in class A. The output must looks like this:

First unique docstring.
Default docstring info:
1
2
3

Second unique docstring.
Default docstring info:
1
2
3

How I can do it?


Solution

  • An instancemethod's docstring is taken from the underlying function, which is why B.A1.__doc__ += constString doesn't work. However:

    B.A1.__func__.__doc__ += constString