假设我有两个 A 类和 B 类:
Class A:
# A's attributes and methods here
Class B:
# B's attributes and methods here
现在我可以评估 B 类对象中 A 的属性,如下所示:
a_obj = A()
b_obj = B(a_obj)
我需要的是双向访问。
如何访问 B 中 A 的属性和 A 中 B 的属性?
您需要以任何一种方式创建指针:
class A(object):
parent = None
class B(object):
def __init__(self, child):
self.child = child
child.parent = self
Now A
可以参考self.parent
(前提是不是None
), and B
可以参考self.child
。如果你尝试创建一个实例A
一个以上的孩子B
,最后一个“父母”获胜。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)