In [ ]:
class Thing(object):
def __init__(self, x):
self.x = x
@property
def x(self):
return self._x
@x.setter
def x(self, value):
if not isinstance(value, (int, float)):
raise ValueError('Invalid x value!')
self._x = value
@property # Python decorator
def y(self):
return self.x**2
In [ ]:
thing = Thing(15.)
In [ ]:
print(thing.x)
In [ ]:
thing = Thing(15.)
In [ ]:
thing.y
In [ ]:
thing.x = 'sup'
In [ ]:
print(thing.x)
In [ ]:
thing.y
In [ ]:
In [ ]: