在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)。 –
謝謝,修正! :) –