0

I would like to reuse a docstring that is used for the same function across multiple classes.

For example

class square:
     def getArea(L,W):
     """ [description]
     L (int) ... 
     W (int) ... 

     return (int) area of shape
     """
     return L*W

 class rectangle:
     def getArea(L,W):
     """ use docstring from above"""
     return L*W

Is there any way to use the same docstring across multiple functions? This is a simple example but some functions have 10 different parameters and being able to use the same docstring will ensure consistency.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
sam
  • 21
  • 1
  • 5
  • Just assign to the `__doc__` attribute manually – jonrsharpe May 03 '21 at 22:11
  • Im sorry can you give me a little more detail. I am new to this. I am hoping to have some sort of central doc (.txt) with all of the shared docstrings and than just update that. – sam May 03 '21 at 23:22
  • See the documentation about [`__doc__`](https://docs.python.org/3/library/types.html?highlight=__doc__#types.ModuleType.__doc__). There are several ways to do what you want, and that's why there isn't 1 single answer to this problem because it depends how you organize your objects. For example, if you reuse 1 single function why not declare it abstract or use it as a mix-in and inherit the docstring? The simple solution is setting the `__doc__` property directly as was said when declaring the class assuming you declare the function somewhere as abstract and having the docstring to reuse. – bad_coder May 05 '21 at 08:34
  • Here is [one good example](https://stackoverflow.com/a/36091548) – bad_coder May 05 '21 at 08:37

0 Answers0