我想让我的代码更有效率(内存).现在我们有很多以可迭代作为参数的函数,例如:
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中的空元组是“常量"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!