在Python中,是否有可能从Bar内部获得包含另一个对象Bar的对象,如Foo?这里是我的意思在python中获取容器/父对象
class Foo(object):
def __init__(self):
self.bar = Bar()
self.text = "Hello World"
class Bar(object):
def __init__(self):
self.newText = foo.text #This is what I want to do,
#access the properties of the container object
foo = Foo()
一个例子是这可能吗?谢谢!
你有一个错字;在'Foo .__ init__'中,'self.bar = Foo()'应该是'self.bar = Bar()'。否则,你有一个无限循环(为了创建一个Foo,你首先必须创建一个Foo)。 –
谢谢,修正! :) –