I have a class that contains an object (set in this case). The set is modified by the standard function operators __add__
and __sub__
, but I also want to explicitly expose a method of getting this set.
Should the @property
decorator be used instead of a simple get()
method, even if there is no intent to allow setting or deleting?
For example:
class A(object):
def __init__(self):
self.attribute = set()
def __add__(self, other):
self.attribute.add(other)
return self
def __sub__(self, other):
self.attribute.remove(other)
return self
@property
def stuff(self):
return self.attribute
It feels semantically correct to use a property and the right error messages are raised if an attempt to set or delete the property occurs AttributeError: can't set attribute
. But, it also feels like a bit of a con to potential users who, knowing that a property exists, may expect the rest of the contract (setting and deleting) to be fulfilled.
Another option might be to define the setter/deleter in terms of __add__
and __sub__
but this seems like a less obvious breach of the same contract.
@stuff.setter
def stuff(self, other):
self.__add__(other)