'How to programmatically add DocString when attribute definition is used in python
I have a template class, which is supposed to be used as a template class for various classes to check values assigned to attributes. It mostly does what it needs to do, but I have not managed to set the Docstrings to the attributes. An extremely simplified version of the class looks like this:
class CreateAttribute:
def __init__(self, value, doc):
self.value = value
self.__doc__ = doc
def __get__(self, instance, owner=None):
return self.value
class Example:
attribute_1 = CreateAttribute(1.4, 'explanation for attribute_1')
ex = Example()
print(ex.attribute_1.__doc__)
This gives:
'Convert a string or number to a floating point number, if possible.'
And as you can see, the Docstring is that of int and not explanation for attribute_1. I understand why this is happening, but I don't have a solution to make it print the Docstring I set.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
