赞
踩
id() 函数用于获取对象的内存地址
a = '3'
b = '3'
print(id(a))
print(id(b))
>
140328813804208
140328813804208
结论: a和b指向相同的内存地址
dis.dis()函数可以查看编辑后的字节码
import dis a = 'if a is not None: pass' b = 'if not a is None: pass' print(dis.dis(a)) print(dis.dis(b)) > 1 0 LOAD_NAME 0 (a) 2 LOAD_CONST 0 (None) 4 COMPARE_OP 9 (is not) 6 POP_JUMP_IF_FALSE 8 >> 8 LOAD_CONST 0 (None) 10 RETURN_VALUE None 1 0 LOAD_NAME 0 (a) 2 LOAD_CONST 0 (None) 4 COMPARE_OP 9 (is not) 6 POP_JUMP_IF_FALSE 8 >> 8 LOAD_CONST 0 (None) 10 RETURN_VALUE None 结论:a和b写法不同,但效果/性能均相同 ```@[TOC](这里写自定义目录标题)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。