简体   繁体   English

如何从另一个类访问更改的变量

[英]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?我想在 A 类更改 self.tt 时在 B().check() 中打印(读取或使用)不断变化的变量 self.tt,是否有任何方法可以做到这一点或其他更好的解决方案来完成任务?

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'

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.请注意, A类的代码保持不变。 However, it's important to understand that it is being used differently.但是,重要的是要了解它的使用方式不同。 A is the class object. A是类对象。 It has attributes that are functions, but no integers.它的属性是函数,但没有整数。 When you create instance a , the functions become methods when you access them with the .当您创建实例a时,当您使用. operator.操作员。 a also has an attribute tt . a也有一个属性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 .函数A.change_ttB.check都需要一个位置参数self However, the bound methods a.change_tt and b.check are no-arg callables.但是,绑定方法a.change_ttb.check是无参数可调用对象。 The process of binding a function to an instance with the .将函数绑定到实例的过程. operator creates a wrapper that passes in self automatically.运算符创建一个自动传入self的包装器。

When you do self.tt += 1 , the object that is the previous value of tt is unbound from tt and possibly garbage collected.当您执行self.tt += 1时,作为tt先前值的对象与tt未绑定,并且可能会被垃圾收集。 Integers are immutable, which means that what really happens here is self.tt = self.tt + 1 .整数是不可变的,这意味着这里真正发生的是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 .这意味着即使tt存在于A中, B.__init__中的语句self.x = A.tt也是不合理的。 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. x将是对作为tt初始值的对象的引用,并且即使tt更改为递增版本,也会继续引用该对象。

An instance of B needs to know about the object that A.tt refers to currently. B的一个实例需要知道A.tt当前引用的对象。 One way to do that is to pass a reference to B.__init__ .一种方法是传递对B.__init__的引用。 That's why we define __init__(self, a) , and invoke B as B(a) to get an instance that refers to a .这就是我们定义__init__(self, a)并将B调用为B(a)以获取引用a的实例的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM