首页 > 解决方案 > 从类中的变量访问类

问题描述

例如:

class Foo:
    def __init__(self):
        self.bar = []


class Baz():
    pass


test = Foo()
test.bar.append(
    Baz()
)

现在我已经实例化Baz了,我该如何test访问Baz

标签: pythonpython-3.x

解决方案


如果您希望像这样设置事物,则需要给每个Baz参考:test

class Baz():
    def __init__(self, test):
        self.test = test    

test = Foo()
test.bar.append(
    Baz(test)

推荐阅读