I have a classes a and b as below:
class a(object):
counter=10
def __init__(self):
self.counter=1
def process(self):
self.counter+=1
return self.counter
class b(a):
def __init__(self,a_obj):
self.counter=a_obj.counter
def process(self):
result=self.counter
return (result)
My so called main function is as :
a_object=a()
b_object=b(a)
for x in range(1,6):
print(a_object.process())
print(b_object.process())
My results for which are as below:
2
3
4
5
6
10
But I want to have access to the last updated value of counter in A i.e. 6 (referencing the instance variable) rather than 10 (which is the value of the class variable).
Please advise where am I going wrong with this.
new_var = b_object.process()? ...atobconstructor when you probably want to passa_objectb(a_object), notb(a).a_obj.counteris a immutable object. Keeping a reference to it is pointless, keep a reference toa_objinstead.