简体   繁体   English

从类中的变量访问类

[英]Accessing class from variable within class

For example:例如:

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


class Baz():
    pass


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

Now that I have instantiated Baz , how can I access test from Baz ?现在我已经实例化了Baz ,我如何从Baz访问test

You would need to give each Baz a reference to test if you wanted things to be set up like this:如果你想像这样设置事情,你需要给每个Baz一个参考来test

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM