python2和python3关于列表推导的差别

看下面两个例子:

python2的环境:列表中的同名变量名被替换了

>>> x = 'my precious'
>>> dummy = [x for x in 'ABC']
>>> dummy
['A', 'B', 'C']
>>> x
'C'

python3:列表中的同名变量名没有被替换

>>> x = 'ABC'
>>> dummy = [ord(x) for x in x]
>>> x
'ABC'
>>> dummy
[65, 66, 67]

总结:在python3环境,对于列表推导、生成器表达式、字典推导、set推导,python3会维护自己的局部变量,局部变量不会影响上下同名的变量;

上一篇:Linux tomcat启动慢, Creation of SecureRandom instance for session ID generation using [SHA1PRNG]took [xx] mil


下一篇:函数的返回值是void