作者:天涯小犁_640 | 来源:互联网 | 2023-05-25 15:41
当我写一些代码时,我发现了一件有趣的事情:
def test():
l = []
for i in range(10):
def f():pass
print(f)
#l.append(f)
test()
import dis
dis.dis(test)
输出是:
.f at 0x7f46c0b0d400>
.f at 0x7f46c0b0d488>
.f at 0x7f46c0b0d400>
.f at 0x7f46c0b0d488>
.f at 0x7f46c0b0d400>
.f at 0x7f46c0b0d488>
.f at 0x7f46c0b0d400>
.f at 0x7f46c0b0d488>
.f at 0x7f46c0b0d400>
.f at 0x7f46c0b0d488>
6 0 BUILD_LIST 0
3 STORE_FAST 0 (l)
7 6 SETUP_LOOP 42 (to 51)
9 LOAD_GLOBAL 0 (range)
12 LOAD_CONST 1 (10)
15 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
18 GET_ITER
>> 19 FOR_ITER 28 (to 50)
22 STORE_FAST 1 (i)
8 25 LOAD_CONST 2 ()
28 LOAD_CONST 3 ('test..f')
31 MAKE_FUNCTION 0
34 STORE_FAST 2 (f)
9 37 LOAD_GLOBAL 1 (print)
40 LOAD_FAST 2 (f)
43 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
46 POP_TOP
47 JUMP_ABSOLUTE 19
>> 50 POP_BLOCK
>> 51 LOAD_CONST 0 (None)
54 RETURN_VALUE
什么时候
def test():
l = []
for i in range(10):
def f():pass
print(f)
l.append(f)
test()
import dis
dis.dis(test)
输出是:
.f at 0x7ff88ffe0400>
.f at 0x7ff88ffe0488>
.f at 0x7ff88ffe0510>
.f at 0x7ff88ffe0598>
.f at 0x7ff88ffe0620>
.f at 0x7ff88ffe06a8>
.f at 0x7ff88ffe0730>
.f at 0x7ff88ffe07b8>
.f at 0x7ff88ffe0840>
.f at 0x7ff88ffe08c8>
6 0 BUILD_LIST 0
3 STORE_FAST 0 (l)
7 6 SETUP_LOOP 55 (to 64)
9 LOAD_GLOBAL 0 (range)
12 LOAD_CONST 1 (10)
15 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
18 GET_ITER
>> 19 FOR_ITER 41 (to 63)
22 STORE_FAST 1 (i)
8 25 LOAD_CONST 2 ()
28 LOAD_CONST 3 ('test..f')
31 MAKE_FUNCTION 0
34 STORE_FAST 2 (f)
9 37 LOAD_GLOBAL 1 (print)
40 LOAD_FAST 2 (f)
43 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
46 POP_TOP
10 47 LOAD_FAST 0 (l)
50 LOAD_ATTR 2 (append)
53 LOAD_FAST 2 (f)
56 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
59 POP_TOP
60 JUMP_ABSOLUTE 19
>> 63 POP_BLOCK
>> 64 LOAD_CONST 0 (None)
67 RETURN_VALUE
如果STORE_FAST
"缓存"了f
,为什么在第一个代码片段中,地址f
是交替的?
在第二个片段中,它有两个LOAD_FAST
,结果是正常的.
LOAD_FAST/STORE_FAST做了一些不知情的事吗?
1> Ashwini Chau..:
这种情况正在发生,因为在每次交替迭代中,重新声明当前的旧函数对象f
没有引用,因此它被垃圾收集,Python可以在下一次迭代中重用该内存空间.另一方面,在第二个列表中,列表指的是每个函数,因此它们永远不会被垃圾回收.
这是一个依赖于实现的东西,CPython的垃圾收集基于引用计数.在PyPy上,输出是不同的:
$ ~/pypy-2.4.0-linux64/bin# ./pypy
Python 2.7.8 (f5dcc2477b97, Sep 18 2014, 11:33:30)
[PyPy 2.4.0 with GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> def test():
.... for i in range(10):
.... def f(): pass
.... print f
....
>>>> test()