<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 中获取容器/父对象

        Getting container/parent object from within python(从 python 中获取容器/父对象)

      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 中获取容器/父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在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>
                          <tbody id='hsqlA'></tbody>
                      • <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>