小编Sol*_*une的帖子

如何获得Python Object Parent?

所以,我试图得到一个自定义对象在'内部'的对象.这是一个例子.

假设o是一个对象 - 它不管是什么类型,它只能存储变量.

o = Object()
class Test():
    def __init__(self):
        self.parent = o ## This is where I fall off; I want to be able to access
                        ## the o object from within the Test object

o.test = Test()
Run Code Online (Sandbox Code Playgroud)

那么,我如何从Test内部获得o?我知道我可以编写Test函数来传递它

o.test = Test(o)
Run Code Online (Sandbox Code Playgroud)

但我宁愿从类定义中做到这一点.

python class object parent

3
推荐指数
3
解决办法
3万
查看次数

标签 统计

class ×1

object ×1

parent ×1

python ×1