[英]How to use function variable of one class to another class in Python
如何在不创建孩子 class 的情况下访问或使用另一个 class 的变量 class
这是代码示例。
class A:
def testA():
self.a = "class A"
class B:
def testB():
**"Here I want to access self.a of class A"**
您需要 A 的一个实例来访问它的变量。
class A:
def testA(self):
self.a = "class A"
class B:
def __init__(self, a):
self.a = a
def testB(self):
print(self.a.a) # access the 'a' attribute of the local A class instance named 'a'
a = A()
a.testA()
b = B(a) # <-- here
b.testB()
没有创造他们的孩子 class
您没有使用 inheritance,因此没有“子类”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.