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

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

        <tfoot id='sSAx9'></tfoot>
      1. <legend id='sSAx9'><style id='sSAx9'><dir id='sSAx9'><q id='sSAx9'></q></dir></style></legend>
      2. Python中的空元组是“常量"吗?

        Is the empty tuple in Python a quot;constantquot;(Python中的空元组是“常量吗?)
          <legend id='ZFvZY'><style id='ZFvZY'><dir id='ZFvZY'><q id='ZFvZY'></q></dir></style></legend>

              <tbody id='ZFvZY'></tbody>
          1. <small id='ZFvZY'></small><noframes id='ZFvZY'>

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

                  <bdo id='ZFvZY'></bdo><ul id='ZFvZY'></ul>

                  <tfoot id='ZFvZY'></tfoot>
                  本文介绍了Python中的空元组是“常量"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想让我的代码更有效率(内存).现在我们有很多以可迭代作为参数的函数,例如:

                  I want to make my code more (memory-)efficient. Right now we have a lot of functions that take an iterable as parameter like:

                  def foo(para,meter,iterable):
                      #...
                      pass
                  

                  有时我们必须为它提供一个空列表才能正常工作:foo(14,25,[]).问题是每次构造一个新列表时:它需要在堆上分配,并且一个列表似乎是 64 字节的内存(在我自己的机器上,用 sys.getsizeof([])) 而只有空元组占用(可能一次)48 个字节.

                  and sometimes we have to provide it an empty list to do its work properly: foo(14,25,[]). The problem is that each time a new list is constructed: it requires to allocate on the heap, and a list seems to 64 bytes of memory (on my own machine, tested with sys.getsizeof([])) whereas the empty tuple only takes a (potentially one time) 48 bytes.

                  因此我想知道空元组是否是一个常数.由于元组是不可变的,因此可以轻松地将长度为 0 的元组(因此 ())设为程序中的常量.这将减少构造时间"(没有,因为它只会设置对常量的引用)并减少分配的内存量.

                  I was therefore wondering whether the empty tuple is a constant. Since tuples are immutable, one can easily make the tuple with length 0 (so ()) a constant in the program. This would decrease the "construction time" (well there is none since it only would set a reference to the constant) and reduce the amount of memory allocated.

                  我的问题是,是否有关于 Python 解释器(即任何流行的解释器)的保证,即空元组确实是一个常量,这样 () 不需要构建时间,也不需要分配额外的内存.

                  My question is whether there are guarantees regarding the Python interpreter (that is any popular interpreter) that the empty tuple is indeed a constant such that () does not require construction time nor allocates additional memory.

                  id(..) 测试它似乎支持确实只有一个零元组的理论:

                  Testing it with id(..) seems to support the theory that there is indeed only one zero-tuple:

                  >>> id(())
                  140290183798856
                  >>> a = ()
                  >>> id(a)
                  140290183798856
                  

                  但有可能在运行时 Python 解释器出于某种原因分叉了元组.

                  but it could be possible that at runtime the Python interpreter forks the tuple for some reason.

                  推荐答案

                  在 CPython 中,空元组是单例.永远只创建一个副本,然后在您使用 () 或在空生成器上使用 tuple() 时重复使用.

                  In CPython, the empty tuple is a singleton. Only one copy is created, ever, then reused whenever you use () or use tuple() on an empty generator.

                  PyTuple_new() 函数 本质上是这样做的:

                  The PyTuple_new() function essentially does this:

                  if (size == 0 && free_list[0]) {
                      op = free_list[0];
                      Py_INCREF(op);
                      // ...
                      return (PyObject *) op;
                  }
                  

                  所以如果元组大小为 0(空)并且 free_list[0] 对象存在(现有的空元组单例),就使用它.

                  So if the tuple size is 0 (empty) and free_list[0] object exists (the existing empty tuple singleton), just use that.

                  请参阅如何在 CPython 中实现元组?了解free_list 的更多细节;CPython 还将重用已创建的 tuple 实例,长度不超过 20.

                  See How is tuple implemented in CPython? for more details on free_list; CPython will also re-use already-created tuple instances up to length 20.

                  这是一个实现细节.其他实现(Jython、IronPython、PyPy)不必这样做.

                  This is an implementation detail. Other implementations (Jython, IronPython, PyPy) do not have to do the same.

                  这篇关于Python中的空元组是“常量"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

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

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

                          <legend id='ezQeR'><style id='ezQeR'><dir id='ezQeR'><q id='ezQeR'></q></dir></style></legend>
                            <tbody id='ezQeR'></tbody>

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