Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I access the changed variable from another class

I want to print(read or use) the changing variable self.tt in B().check() while class A is changing self.tt, is it any way to do it or other better solution to achieve the task?

class A:
    def __init__(self):
        self.tt = 0

    def change_tt(self):
        for i in range(100000000):
            self.tt += 1
            sleep(1)


class B():
    def __init__(self):
        self.x = A.tt

    def check(self):
        while True:
            print(self.x)


a = A()
b = B()

x1 = threading.Thread(target=A.change_tt)
x2 = threading.Thread(target=B.check)

x1.start()
x2.start()
AttributeError: type object 'A' has no attribute 'tt'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are a couple of things going on here. The biggest issue is that you can’t access an instance attribute from the class. A secondary issue is that integers are immutable.

Here is some code that should fix both:

class A:
    def __init__(self):
        self.tt = 0

    def change_tt(self):
        for i in range(100000000):
            self.tt += 1
            sleep(1)


class B():
    def __init__(self, a):
        self.a = a

    def check(self):
        while True:
            print(self.a.tt)


a = A()
b = B(a)

x1 = threading.Thread(target=a.change_tt)
x2 = threading.Thread(target=b.check)

x1.start()
x2.start()

Notice that the code of class A remains unchanged. However, it’s important to understand that it is being used differently. A is the class object. It has attributes that are functions, but no integers. When you create instance a, the functions become methods when you access them with the . operator. a also has an attribute tt.

A thread target should be a no-arg callable. The functions A.change_tt and B.check both require a single positional argument, self. However, the bound methods a.change_tt and b.check are no-arg callables. The process of binding a function to an instance with the . operator creates a wrapper that passes in self automatically.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading