Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying a python docstring with a decorator: Is it a good idea?

Tags:

python

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?

like image 449
alexis Avatar asked Mar 16 '12 21:03

alexis


1 Answers

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.

like image 72
jfs Avatar answered Nov 15 '22 10:11

jfs