You're thinking in the right direction but you should be looking at Python properties
to do what you're doing:
class Foo():
def __init__(self, x):
self.__x = x
@property
def x(self):
return self.__x
@x.setter
def set_x(self, value):
if(value < 0):
self.__x = value
Wherein you can nest the "set" logic and the "get" logic (if you're changing how to present it.) This makes calling the value x
from the class feel as if you're accessing attributes.
foo = Foo(-4)
foo.x
>>> -4
foo.x = 3
foo.x
>>> -4
foo.x = -12
foo.x
>>> -12