I noticed that if you have a property
in your class with a setter
function, it won't work unless you subclass something. Here's an example:
This does not print anything to the output, and does not set the value
class MyClass():
def __init__(self):
self.tax = 3
@property
def cost(self):
self._cost = 15
return self._cost - self.tax
@cost.setter
def cost(self, value):
self._cost = value
print "New Cost: {}".format(self._cost - self.tax)
mycls = MyClass()
mycls.cost = 55
But if I subclass object
, it works fine:
class MyClass(object):
def __init__(self):
self.tax = 3
@property
def cost(self):
self._cost = 15
return self._cost - self.tax
@cost.setter
def cost(self, value):
self._cost = value
print "New Cost: {}".format(self._cost - self.tax)
mycls = MyClass()
mycls.cost = 55
>>> New Cost: 52
Why exactly is this? And what is object
?