A python docstring must be given as a literal string; but sometimes it's useful to have similar docstrings for several functions (e.g., different constructors), or several access methods might accept the same list of arguments (and then rely on the same hidden method), so it would be nice to use the same description everywhere. For such cases I can construct a docstring by assigning to __doc__
, which I do by means of a simple decorator. The system works very nicely (in python 2), and I'm pleased with how simple, clear and well-encapsulated it is.
The question: Is this a good idea? In particular, are there tools that would be confused by this set-up (e.g., anything that extracts docstrings from the source rather than from the bytecode). Is the solution still going to work in python 3? Are there other reasons or circumstances that would make this inadvisable?
It should not break any tools and it should work on Python 3.
It is ok If it doesn't hurt a source code readability i.e., you can still find out what the function does and how to use it.
The problem might be that it masks a poor design. If several methods use the same list of arguments the code should be refactored (create an object that works with the list) rather than patched by generating repetitive docstrings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With