python的空元组有没有意义,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

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.

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.

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.

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

>>> id(())

140290183798856

>>> a = ()

>>> id(a)

140290183798856

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

解决方案

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.

The PyTuple_new() function essentially does this:

if (size == 0 && free_list[0]) {

op = free_list[0];

Py_INCREF(op);

// ...

return (PyObject *) op;

}

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

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值