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

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

    2. <tfoot id='m3jne'></tfoot>
        <bdo id='m3jne'></bdo><ul id='m3jne'></ul>

        python什么时候删除变量?

        时间:2023-07-05

        <tfoot id='vcokC'></tfoot>

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

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

                  本文介绍了python什么时候删除变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道 python 有一个自动垃圾收集器,因此它应该在不再引用变量时自动删除变量.

                  I know that python has an automatic garbage collector and so it should automatically delete variables when there are no more reference to them.

                  我的印象是局部变量(函数内部)不会发生这种情况.

                  My impression is that this does not happen for local variables (inside a function).

                  def funz(z):
                      x = f(z) # x is a np.array and contains a lot of data
                      x0 = x[0]
                      y = f(z + 1) # y is a np.array and contains a lot of data
                      y0 = y[0]
                  
                      # is x and y still available here?
                      return y0, x0
                  

                  del x是节省内存的正确方法吗?

                  Is del x the right way to save memory?

                  def funz(z):
                      x = f(z) # x is a np.array and contains a lot of data
                      x0 = x[0]
                      del x
                      y = f(z + 1) # y is a np.array and contains a lot of data
                      y0 = y[0]
                      del y
                  
                      return y0, x0
                  

                  我已经编辑了我的示例,使其更类似于我的实际问题.在我的真正问题中,x 和 y 不是列表,而是包含不同大型 np.array 的类.

                  I have edited my example such that it is more similar to my real problem. In my real problem x and y are not lists but classes that contain different large np.array.

                  我能够运行代码:

                  x = f(z)
                  x0 = x[0]
                  print(x0)
                  
                  y = f(z + 1)
                  y0 = [0]
                  print(y0)
                  

                  推荐答案

                  实现使用引用计数来确定何时应该删除变量.

                  Implementations use reference counting to determine when a variable should be deleted.

                  变量超出范围后(如您的示例),如果没有剩余的引用,则内存将被释放.

                  After the variable goes out of scope (as in your example) if there are no remaining references to it, then the memory will be freed.

                  def a():
                      x = 5 # x is within scope while the function is being executed
                      print x
                  
                  a()
                  # x is now out of scope, has no references and can now be deleted
                  

                  除了列表中的字典键和元素之外,通常很少有理由在 Python 中手动删除变量.

                  Aside from dictionary keys and elements in lists, there's usually very little reason to manually delete variables in Python.

                  不过,正如对这个问题的回答中所说,使用 del可用于显示意图.

                  Though, as said in the answers to this question, using del can be useful to show intent.

                  这篇关于python什么时候删除变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 中强制垃圾收集以释放内存 下一篇:python中的引用循环是什么?

                  相关文章

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

                    <tfoot id='NvWqP'></tfoot>

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