This question deals with __iadd__ on Python read-write properties. However, I'm struggling to find the solution for read-only properties.
In my MWE we have a read-only property Beta.value, returning an Alpha instance. I imagine I should be able to use __iadd__ on Beta.value because the returned value is mutated in-place, and no change is made to Beta itself, much like the "beta.value.content +=" line preceding it. However the following code crashes with an AttributeError: can't set attribute.
Is it possible to use __iadd__ on read-only properties?
class Alpha:
def __init__( self, content : int ) -> None:
self.content : int = content
def __iadd__( self, other : int ) -> "Alpha":
self.content += other
return self
class Beta:
def __init__( self ):
self.__value: Alpha = Alpha(1)
@property
def value( self ) -> Alpha:
return self.__value
beta = Beta()
beta.value.content += 2
beta.value += 2