<tfoot id='MQp6t'></tfoot>

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

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

      1. <legend id='MQp6t'><style id='MQp6t'><dir id='MQp6t'><q id='MQp6t'></q></dir></style></legend>

        复合对象上的python垃圾收集器行为

        python garbage collector behavior on compound objects(复合对象上的python垃圾收集器行为)
            <bdo id='tSdUj'></bdo><ul id='tSdUj'></ul>

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

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

                1. <i id='tSdUj'><tr id='tSdUj'><dt id='tSdUj'><q id='tSdUj'><span id='tSdUj'><b id='tSdUj'><form id='tSdUj'><ins id='tSdUj'></ins><ul id='tSdUj'></ul><sub id='tSdUj'></sub></form><legend id='tSdUj'></legend><bdo id='tSdUj'><pre id='tSdUj'><center id='tSdUj'></center></pre></bdo></b><th id='tSdUj'></th></span></q></dt></tr></i><div id='tSdUj'><tfoot id='tSdUj'></tfoot><dl id='tSdUj'><fieldset id='tSdUj'></fieldset></dl></div>
                    <tbody id='tSdUj'></tbody>
                  <tfoot id='tSdUj'></tfoot>
                  本文介绍了复合对象上的python垃圾收集器行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果复合对象的某些部分仍然被引用,python 垃圾收集器是否会清理它

                  Does python garbage collector cleans up a compound object if some of its parts are still referenced

                  例如

                  def foo():
                      A = [ [1, 3, 5, 7], [2, 4, 6, 8]]
                      return A[1]
                  B = foo()
                  

                  A[0] 会被垃圾回收吗?

                  有没有办法通过代码确认?

                  Is there a way to confirm the same through code?

                  推荐答案

                  Nothing 引用列表 A 和嵌套列表 A[0],所以是的,它们会从内存中删除.

                  Nothing references the list A and the nested list A[0], so yes, they will be deleted from memory.

                  A[1] 引用的嵌套列表对象与原始容器没有连接.

                  The nested list object referenced by A[1] has no connection back to its original container.

                  请注意,执行此操作的不是垃圾收集器;GC 只处理破坏循环引用.这种简单的情况完全由引用计数来处理.

                  Note that it's not the garbage collector that does this; the GC only deals in breaking circular references. This simple case is handled entirely by reference counting.

                  foo() 返回的那一刻,本地命名空间被清除.这意味着 A 被删除,这意味着列表对象引用计数下降到 0.这将清除该列表对象,这意味着 包含 列表也看到它们的引用计数下降一.对于 A[0] 这意味着计数也下降到 0 并被清除.

                  The moment foo() returns, the local namespace is cleared up. That means A is removed, which means the list object reference count drops to 0. This clears that list object, which means that the contained lists also see their reference count drop by one. For A[0] that means the count drops to 0 too and it is cleared.

                  对于由 A[1] 引用的列表对象,您现在有一个对它的引用 B,所以它的计数仍然是 1 并且它仍然是活动的".

                  For the list object referenced by A[1], you now have a reference B to it, so it's count is still 1 and it remains 'alive'.

                  要通过代码确认相同,只需使用带有 list 的子类.__del__">__del__ 方法 让我们知道对象何时被删除:

                  To confirm the same through code, simply use a subclass of list with a __del__ method to let us know when the object is being deleted:

                  >>> class DelList(list):
                  ...     def __del__(self):
                  ...         print 'Deleted {}'.format(self)
                  ... 
                  >>> def foo():
                  ...     A = DelList([DelList([1, 3, 5, 7]), DelList([2, 4, 6, 8])])
                  ...     return A[1]
                  ... 
                  >>> B = foo()
                  Deleted [[1, 3, 5, 7], [2, 4, 6, 8]]
                  Deleted [1, 3, 5, 7]
                  >>> del B
                  Deleted [2, 4, 6, 8]
                  

                  所有这些都特定于 CPython(参考 Python 实现);其他实现可能会以不同的方式处理对象的生命周期(例如,确实使用垃圾收集器在扫描中销毁对象),但 AA[0] 的生命周期不会改变那些案件;GC 仍会在其他实现中收集它们,尽管可能在不同的时间点.

                  All this is specific to CPython (the reference Python implementation); other implementations may handle object lifetimes differently (e.g. do use a garbage collector to destroy objects in sweeps), but the lifetime of A and A[0] doesn't change in those cases; the GC will still collect those in other implementations, albeit at a different point in time perhaps.

                  这篇关于复合对象上的python垃圾收集器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                    <bdo id='dXAPG'></bdo><ul id='dXAPG'></ul>

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

                        2. <legend id='dXAPG'><style id='dXAPG'><dir id='dXAPG'><q id='dXAPG'></q></dir></style></legend>

                              <tbody id='dXAPG'></tbody>

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

                            <tfoot id='dXAPG'></tfoot>