Can you explain why you want to do this?
You're playing around with instance variables/attributes which won't migrate from one class to another (they're bound not even to ClassA
, but to a particular instance of ClassA
that you created when you wrote ClassA()
). If you want to have changes in one class show up in another, you can use class variables:
class ClassA(object):
var1 = 1
var2 = 2
@classmethod
def method(cls):
cls.var1 = cls.var1 + cls.var2
return cls.var1
In this scenario, ClassB
will pick up the values on ClassA
from inheritance. You can then access the class variables via ClassA.var1
, ClassB.var1
or even from an instance ClassA().var1
(provided that you haven't added an instance method var1
which will be resolved before the class variable in attribute lookup.
I'd have to know a little bit more about your particular use case before I know if this is a course of action that I would actually recommend though...