【发布时间】:2012-06-03 05:31:47
【问题描述】:
在 Python 中,是否可以从 Bar 自身中获取包含另一个对象 Bar 的对象,比如 Foo?这是我的意思的一个例子
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)。
标签: python class object containers