I am using descriptors to define the registers of an interface class:
class Register(object):
def __init__(self, address, docstring="instance docstring"):
self.address = address
self.__doc__ = docstring
def __get__(self, obj, objtype):
return obj.read(self.address)
def __set__(self, obj, val):
return obj.write(self.address, val)
class Interface(object):
r = Register(0x00, docstring="the first register")
I wold like the user of ipython to be able to do one of the following:
i = Interface()
i.r? #should show the docstring "the first register"
or
i = Interface()
i.r( #should show the docstring "the first register" when parentheses are opened
However, the docstring is always the one from the int object that obj.read returns, not the specified docstring. Is there a way to show the right docstring in this situation?
If I am not using descriptors but manually define them, it works when the parentheses are open:
class Interface(object):
@property
def r(self):
"""this docstring will be shown alternatively"""
return self.read(0x0)
@r.setter
def r(self,v):
"""this is the docstring that is shown"""
self.write(0x0,v)
i = Interface()
i.r( #the right docstring pops up here once i open the bracket
If the setter does not define a docstring, the one of the getter is shown when brackets are opened.
Can I somehow get the same behaviour by using descriptors without unreasonable overhead?
My question is somewhat similar to this one, which however gives no satisfactory answer: Creating dynamic docstrings in Python descriptor