繁体   English   中英

从python中获取容器/父对象

[英]Getting container/parent object from within python

在 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()

这可能吗? 谢谢!

传递对Bar对象的引用,如下所示:

class Foo(object):
    def __init__(self):
        self.text = "Hello World"  # has to be created first, so Bar.__init__ can reference it
        self.bar = Bar(self)

class Bar(object):
    def __init__(self, parent):
        self.parent = parent
        self.newText = parent.text

foo = Foo()

编辑:正如@thomleo指出的那样,这可能导致垃圾回收问题。 建议的解决方案位于http://eli.thegreenplace.net/2009/06/12/safely-using-destructors-in-python/中 ,看起来像

import weakref

class Foo(object):
    def __init__(self):
        self.text = "Hello World"
        self.bar = Bar(self)

class Bar(object):
    def __init__(self, parent):
        self.parent = weakref.ref(parent)    # <= garbage-collector safe!
        self.newText = parent.text

foo = Foo()

是否有可能从Bar本身中获取包含另一个对象Bar的对象(例如Foo)?

不是“自动”的,因为该语言不是那样构建的,尤其是,该语言的构建方式使得无法保证Foo存在。

也就是说,您始终可以明确地执行此操作。 就像Python中的其他所有标识符一样,属性只是名称,而不是数据的存储空间。 因此,没有什么可以阻止您让Bar实例具有一个手动分配的foo属性,该属性是Foo实例,反之亦然。

是的,有可能。 即使没有在创建对象时传递容器引用,即对象是否为类属性。 您的对象需要实现描述符协议(具有__get__() ):

class ChildName(SimpleNamespace):                                                         

    def __get__(self, instance, owner):
        # instance is our parent
        return f'I am {self.name}, my parent is {instance.name}.'


class ChildDiff(SimpleNamespace):

    @property
    def diff(self):
        return self.born - self.parent.born

    def age_diff(self):
        return f'I am {self.diff} years older than {self.parent.name}.'

    def __get__(self, instance, owner):
        self.parent = instance  # XXX: weakref?
        return self  # expose object to be able call age_diff() etc.


class Parent(SimpleNamespace):

    child_name = ChildName(name='Bar')
    child_diff = ChildDiff(born=42)


parent = Parent(name='Foo', born=23)
print(parent.child_name)             # ... I am Bar, my parent is Foo.
print(parent.child_diff.age_diff())  # ... I am 19 years older than Foo.

这对我有用......

######## 父母########

进口孩子

obj_1 = 25 # 父子都可以访问的对象

def startup(): # 任何启动函数

global obj_1
child.ref( obj_1 )       # send the shared object to the child
.
.
.

##########孩子############

obj_1 = 0 # 初始值将被覆盖

def ref(shared_obj): # 接收对共享对象的引用

global  obj_1         # shared object is global in the child, optional

obj_1 = shared_obj    # set obj_1 in the child to be obj_1 in the parent

那么使用继承呢:

class Bar(object):
    def __init__(self):
        self.newText = self.text

class Foo(Bar):
    def __init__(self):
        self.txt = 'Hello World'
        Bar.__init__(self)

foo = Foo()
print foo.newText

暂无
暂无

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

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