[英]Using a variable from one class to another one in python
I want to use a variable from class A for some computation in class B. I,m not sure that I use the self.out from the class A in class B appropriately?我想使用 class A 中的变量在 class B 中进行一些计算。我不确定我是否在 class B 中适当地使用了 class A 中的 self.out?
Class A: Class答:
class A(nn.Module):
def __init__(self):
super(A, self).__init__()
self.out = func()
Class B: Class乙:
class B(nn.Module):
def __init__(self):
super(A, self).__init__()
self.result = function_1() + A.self.out
Maybe this is what you need.也许这就是您所需要的。 I made a small example of what I understood.
我举了一个我理解的小例子。 These "prints" were placed to improve the understanding that Class "C" can fetch any function or variable from the other parent classes.
放置这些“印刷品”是为了提高对 Class“C”可以从其他父类获取任何 function 或变量的理解。
class A():
def __init__(self):
variable = None
def test(self, number):
return f'another class {number}'
class B():
def __init__(self):
self.data = None
self.out = self.print_data(5)
def print_data(self, number):
return number
def print_elem(self):
return self.data
class C(A, B):
def __init__(self):
super().__init__()
c = C()
print(c.print_data(8))
print(c.out)
c.data = 100
print(c.print_elem())
print(c.test(3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.