I have two classes: one with an override of the in-place operator (say +=), and the other an instance of the first with @property. (Note: this greatly simplifies from my actual code to a minimum that reproduces the problem.)
class MyValue(object):
def __init__(self, value):
self.value = value
def __iadd__(self, other):
self.value += other
return self
def __repr__(self):
return str(self.value)
class MyOwner(object):
def __init__(self):
self._what = MyValue(40)
@property
def what(self):
return self._what
Now when I try to use this operator for a public property:
>>> owner = MyOwner()
>>> owner.what += 2
AttributeError: can't set attribute
From what I found, this is to be expected, since it is trying to set the property on owner. Is there a way to prevent setting the property on a new object, but still allowing me (in place) to modify the object behind it, or is it just a fad of the language?
(. , , , , , Python 3.)
, .
class MyValue(object):
def add(self, other):
self.value += other
>>> owner = MyOwner()
>>> owner.what.add(2)
>>> print(owner.what)
42