I have this code:
class X(object):
x = 10
def test_x(self):
return self.x
class Y(X):
def test_y(self):
return self.x
y = Y()
y.test_y() # works fine
But when I construct a new object z based on X using type :
z = type('Z', (X,), dict(z=1))
z.x # works fine
z.test_x() # gives a TypeError :
unbound method test_x() must be called with Z instance as first argument (got nothing instead).
How can I solve it.
UPDATE
With the help and understanding of Martijn, this is how I solved it:
z = type('Z', (X,), dict(z=1))()
z.test_x()
zis a class, not an instance. Create an instance instead:What you did was the equivalent of: