Is there a way in Python 3.X to check if a class defines a setter method for one of its properties?
Let's say I have the following example :
class Test:
def __init__(self):
self._p = None
@property
def my_property(self):
return self._p
@my_property.setter
def my_property(self, new_value):
self._p = new_value
I can check if my_property
is defined with hasattr
but I can't find a way to check if the setter is defined.
Here are the two avenues I considered without success :
- Using
hasattr
to find if a method namedmy_property.setter
or something like that exists. - Using the
inspect.signature
function to see if the methodmy_property
has 2 parameters (self and new_value).
I guess that an extended answer to this question would consider how to detect the presence of a getter as well (making the difference between a property and a getter method because callable(test.my_property)
returns False
when we might think it should be True
because it is a method).