在 init (Python 2.7) 之后传递继承 class 的实例
passing instance of inherited class after init (Python 2.7)
在 class b
被 class c
继承并调用之后,我正在尝试访问另一个 class a
中 class b
(self.variable) 的对象class a
的实例。我不知道如何访问在继承初始化期间创建的 class b
的实例。此外,a 需要继承自 class k
.
如果我可以在 class a
中访问 self.variable
并进一步传递它而不显式引用 class b
的实例(即,只需使用 self.variable
而不是 b.variable
) 并且没有更改我已经分配给 class c
中的变量的值(所以没有重新初始化它)
如果有任何帮助,我将不胜感激,我已经为此工作了几个小时。
class k(object):
def __init__(self):
pass
class b(object):
def __init__(self):
self.variable = 1
class c(b):
def __init__(self):
b.__init__(self)# b is inherited from & initialized
# alternative for inheritance
#super(c,self).__init__()
print("init(): " + str(self.variable))
def run(self):
self.variable = 2
print("run(): " + str(self.variable))
a()# here I need to pass the instance of b
class a(k):
def __init__(self):
k.__init__(self)# a() needs to inherit from k()
# ERROR: cannot access variables of b()
print("a(): " + str(self.variable))
if __name__ == "__main__":
c().run()
我想我知道怎么做了。
如果传递实例就足够了,这将起作用:
class k(object):
def __init__(self):
pass
class b(object):
def __init__(self):
self.bb = self.bb()
class bb:
def __init__(self):
self.variable = 1
class c(b):
def __init__(self):
b.__init__(self)# b is inherited from & initialized
# alternative for inheritance
#super(c,self).__init__()
print("init(): " + str(self.bb.variable))
def run(self):
self.bb.variable = 99
print("run(): " + str(self.bb.variable))
a(self)# pass instance of b
class a(k):
def __init__(self, bInstance):
k.__init__(self)# a() needs to inherit from k()
print("a(): " + str(bInstance.bb.variable))
if __name__ == "__main__":
c().run()
据我了解,如果我想通过自己到达那里,我需要 class a
继承自 class c
并且 class c
继承并初始化 class b
,最后的调用只是 a()
(它完成了我需要它做的事情,因为 a 继承了我需要的一切):
class k(object):
def __init__(self):
pass
class b(object):
def __init__(self):
self.bb = self.bb()
class bb:
def __init__(self):
self.variable = 1
class c(b):
def __init__(self):
b.__init__(self)# b is inherited from & initialized
# alternative for inheritance
#super(c,self).__init__()
print("init(): " + str(self.bb.variable))
def run(self):
self.bb.variable = 99
print("run(): " + str(self.bb.variable))
class a(k,c):
def __init__(self):
k.__init__(self)# a() needs to inherit from k()
c.__init__(self)
print("a(): " + str(self.bb.variable))
self.run()
print("a() #2: " + str(self.bb.variable))
if __name__ == "__main__":
a()
在 class b
被 class c
继承并调用之后,我正在尝试访问另一个 class a
中 class b
(self.variable) 的对象class a
的实例。我不知道如何访问在继承初始化期间创建的 class b
的实例。此外,a 需要继承自 class k
.
如果我可以在 class a
中访问 self.variable
并进一步传递它而不显式引用 class b
的实例(即,只需使用 self.variable
而不是 b.variable
) 并且没有更改我已经分配给 class c
中的变量的值(所以没有重新初始化它)
如果有任何帮助,我将不胜感激,我已经为此工作了几个小时。
class k(object):
def __init__(self):
pass
class b(object):
def __init__(self):
self.variable = 1
class c(b):
def __init__(self):
b.__init__(self)# b is inherited from & initialized
# alternative for inheritance
#super(c,self).__init__()
print("init(): " + str(self.variable))
def run(self):
self.variable = 2
print("run(): " + str(self.variable))
a()# here I need to pass the instance of b
class a(k):
def __init__(self):
k.__init__(self)# a() needs to inherit from k()
# ERROR: cannot access variables of b()
print("a(): " + str(self.variable))
if __name__ == "__main__":
c().run()
我想我知道怎么做了。 如果传递实例就足够了,这将起作用:
class k(object):
def __init__(self):
pass
class b(object):
def __init__(self):
self.bb = self.bb()
class bb:
def __init__(self):
self.variable = 1
class c(b):
def __init__(self):
b.__init__(self)# b is inherited from & initialized
# alternative for inheritance
#super(c,self).__init__()
print("init(): " + str(self.bb.variable))
def run(self):
self.bb.variable = 99
print("run(): " + str(self.bb.variable))
a(self)# pass instance of b
class a(k):
def __init__(self, bInstance):
k.__init__(self)# a() needs to inherit from k()
print("a(): " + str(bInstance.bb.variable))
if __name__ == "__main__":
c().run()
据我了解,如果我想通过自己到达那里,我需要 class a
继承自 class c
并且 class c
继承并初始化 class b
,最后的调用只是 a()
(它完成了我需要它做的事情,因为 a 继承了我需要的一切):
class k(object):
def __init__(self):
pass
class b(object):
def __init__(self):
self.bb = self.bb()
class bb:
def __init__(self):
self.variable = 1
class c(b):
def __init__(self):
b.__init__(self)# b is inherited from & initialized
# alternative for inheritance
#super(c,self).__init__()
print("init(): " + str(self.bb.variable))
def run(self):
self.bb.variable = 99
print("run(): " + str(self.bb.variable))
class a(k,c):
def __init__(self):
k.__init__(self)# a() needs to inherit from k()
c.__init__(self)
print("a(): " + str(self.bb.variable))
self.run()
print("a() #2: " + str(self.bb.variable))
if __name__ == "__main__":
a()