2

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.

9
  • 1
    do you just want new_var = b_object.process()? ... Commented Jul 19, 2017 at 7:51
  • no, I am looking to access the last updated value in class a (in the example it is last updated as 6) and use it in class b for some operation (as of this example , just print) Commented Jul 19, 2017 at 7:54
  • 1
    You are passing a to b constructor when you probably want to pass a_object Commented Jul 19, 2017 at 7:55
  • 1
    You need to call b(a_object), not b(a). Commented Jul 19, 2017 at 7:56
  • 2
    a_obj.counter is a immutable object. Keeping a reference to it is pointless, keep a reference to a_obj instead. Commented Jul 19, 2017 at 7:57

1 Answer 1

2

There is no point in keeping a reference to a_obj.counter. All you are doing is creating a new reference to an immutable object. You are not referencing the attribute on a_obj, only the object that a_obj.counter also points to.

But when a.process() is run, the counter reference is replaced by one that points to a different integer object. b.counter still references the initial value.

You want to keep a reference to a_obj itself, so you can ask it what the counter attribute there points to whenever you need it:

class b(a):
    def __init__(self, a_obj):
        self.a_obj = a_obj
    def process(self):
        result = self.a_obj.counter
        return result

You may want to read up on how Python names and attributes work; they are merely little name tags tied to the actual object. See Ned Batchelder's excellent Facts and myths about Python names and values article.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, that totally made sense and your explanation also gave me much clarity on the subject. Cheers.
It also worths noting that in original code counter=10 declares it as the class member, not the instance member, which is not necessary and even confusing in this case. Instance fields should be defined in constructor only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.