I'm working on a piece of code that uses Pyhton's magical __getattr___
method to allow for call of 'functions' that are not really defined.
The problem is that help()
returns the wrong docstring, that of the class, instead of that defined using __doc__
. I can, however, print the correct docstring.
What must I do to get help()
working as I want it to do?
Example:
class MathOperations(object):
class VariableFunction(object):
"""This is the standard docstring of the class."""
def __init__(self, name):
if name == 'add':
self.__doc__ = 'Return the sum'
def __getattr__(self, attr):
return self.VariableFunction(attr)
MathOperations = MathOperations()
help(MathOperations.add)
print(MathOperations.add.__doc__)
Which gives:
Help on VariableFunction in module __main__ object:
class VariableFunction(__builtin__.object)
| This is the standard docstring of the class.
|
| Methods defined here:
|
| __init__(self, name)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
Return the sum
I didn't write the original code, but I want to get it running.