It looks like you want to use the properties:
class Foo(object): def __init__(self, m): self._m = m @property def m(self): print 'accessed attribute' return self._m @m.setter def m(self, m): print 'setting attribute' self._m = m >>> f = Foo(m=5) >>> fm = 6 setting attribute >>> fm accessed attribute 6
This is the most adaptive way to access variables, for simple use cases you do not need to do this, and in Python you can always change your design to use properties later at no cost.
source share