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'
CodePudding user response:
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.
When you do self.tt = 1
, the object that is the previous value of tt
is unbound from tt
and possibly garbage collected. Integers are immutable, which means that what really happens here is self.tt = self.tt 1
. That means that the statement self.x = A.tt
in B.__init__
is unreasonable even if tt
existed in A
. x
would be a reference to the object that is the initial value of tt
, and would keep referring to that object even as tt
changed to the incremented version.
An instance of B
needs to know about the object that A.tt
refers to currently. One way to do that is to pass a reference to B.__init__
. That's why we define __init__(self, a)
, and invoke B
as B(a)
to get an instance that refers to a
.