<bdo id='jsSrN'></bdo><ul id='jsSrN'></ul>
    <tfoot id='jsSrN'></tfoot><legend id='jsSrN'><style id='jsSrN'><dir id='jsSrN'><q id='jsSrN'></q></dir></style></legend>

  • <i id='jsSrN'><tr id='jsSrN'><dt id='jsSrN'><q id='jsSrN'><span id='jsSrN'><b id='jsSrN'><form id='jsSrN'><ins id='jsSrN'></ins><ul id='jsSrN'></ul><sub id='jsSrN'></sub></form><legend id='jsSrN'></legend><bdo id='jsSrN'><pre id='jsSrN'><center id='jsSrN'></center></pre></bdo></b><th id='jsSrN'></th></span></q></dt></tr></i><div id='jsSrN'><tfoot id='jsSrN'></tfoot><dl id='jsSrN'><fieldset id='jsSrN'></fieldset></dl></div>

  • <small id='jsSrN'></small><noframes id='jsSrN'>

        从 python 中获取容器/父对象

        时间:2023-11-08

      1. <tfoot id='HX5T3'></tfoot>

          • <bdo id='HX5T3'></bdo><ul id='HX5T3'></ul>

              <tbody id='HX5T3'></tbody>

                  <small id='HX5T3'></small><noframes id='HX5T3'>

                  <i id='HX5T3'><tr id='HX5T3'><dt id='HX5T3'><q id='HX5T3'><span id='HX5T3'><b id='HX5T3'><form id='HX5T3'><ins id='HX5T3'></ins><ul id='HX5T3'></ul><sub id='HX5T3'></sub></form><legend id='HX5T3'></legend><bdo id='HX5T3'><pre id='HX5T3'><center id='HX5T3'></center></pre></bdo></b><th id='HX5T3'></th></span></q></dt></tr></i><div id='HX5T3'><tfoot id='HX5T3'></tfoot><dl id='HX5T3'><fieldset id='HX5T3'></fieldset></dl></div>
                  <legend id='HX5T3'><style id='HX5T3'><dir id='HX5T3'><q id='HX5T3'></q></dir></style></legend>
                • 本文介绍了从 python 中获取容器/父对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  In Python, is it possible to get the object, say Foo, that contains another object, Bar, from within Bar itself? Here is an example of what I mean

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

                  Is this possible? Thanks!

                  解决方案

                  Pass a reference to the Bar object, like so:

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

                  Edit: as pointed out by @thomleo, this can cause problems with garbage collection. The suggested solution is laid out at http://eli.thegreenplace.net/2009/06/12/safely-using-destructors-in-python/ and looks like

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

                  这篇关于从 python 中获取容器/父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:检查容器中是否存在 NaN 下一篇:究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?)

                  相关文章

                  <legend id='hsqlA'><style id='hsqlA'><dir id='hsqlA'><q id='hsqlA'></q></dir></style></legend>

                • <small id='hsqlA'></small><noframes id='hsqlA'>

                    <bdo id='hsqlA'></bdo><ul id='hsqlA'></ul>
                  <tfoot id='hsqlA'></tfoot>
                • <i id='hsqlA'><tr id='hsqlA'><dt id='hsqlA'><q id='hsqlA'><span id='hsqlA'><b id='hsqlA'><form id='hsqlA'><ins id='hsqlA'></ins><ul id='hsqlA'></ul><sub id='hsqlA'></sub></form><legend id='hsqlA'></legend><bdo id='hsqlA'><pre id='hsqlA'><center id='hsqlA'></center></pre></bdo></b><th id='hsqlA'></th></span></q></dt></tr></i><div id='hsqlA'><tfoot id='hsqlA'></tfoot><dl id='hsqlA'><fieldset id='hsqlA'></fieldset></dl></div>