Reputation: 31233
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
?
Upvotes: 0
Views: 51
Reputation: 11420
Analyzing
setattr(Foo, "setx", classmethod(setx_method))
It's equivalent to
@classmethod
def setx_method(self,x):
self.x=x
Now when you invoke f.setx(5)
, it actually binds f
's class(ie, Foo)
with self and 5 with x
and thus it executed Foo.x = 5
In short you have two x
in Foo
, one is instance member x
and one is class member x
.
f.setx(5) # sets class x
f.test() # print class variable x
f.normal_test() # print instance variable x
Upvotes: 1
Reputation: 3940
You shouldn't use @classmethod
with self
, because it points to your class instance, while in @classmethod
you should use cls
as it points to whole your class.
See here: Meaning of @classmethod and @staticmethod for beginner?
So the simplest solution is just remove classmethod
:
setattr(Foo, "test", bar)
setattr(Foo, "setx", setx_method)
Upvotes: 0