I have problem sharing members of a class with its dynamically generated methods.
For example below x
accessed from __init__
and from normal_test
is different fro x
accessed from dynamically bounded methods test
and setx
:
class Foo:
def __init__(self):
self.x = 10
def normal_test(self):
print self.x
def bar(self):
print self.x
def setx_method(self,x):
self.x = x
setattr(Foo, "test", classmethod(bar))
setattr(Foo, "setx", classmethod(setx_method))
f = Foo();
f.setx(5)
f.test()
f.normal_test()
How should I write the code such that self.x
would refer to same x
?