What “is” a fuck in Python | 阿小信的博客
阿小信大人的头像
Where there is a Python, there is a way. 阿小信大人

What “is” a fuck in Python2015-07-01 17:45

CPython的优化

https://github.com/python/cpython/blob/829b49cbd2e4b1d573470da79ca844b730120f3d/Python/peephole.c#L223 https://gist.github.com/mrluanma/9118bf2ada09ebd017d3

>>> x = 'a' * 20
>>> y = 'a' * 20
>>> x is y
True
>>> x = 'a' * 21
>>> y = 'a' * 21
>>> x is y
False
>>> x = 'a' * 21; y = 'a' * 21
>>> x is y
False
>>> x = 'aaaaaaaaaaaaaaaaaaaaa'
>>> len(x)
21
>>> y = 'aaaaaaaaaaaaaaaaaaaaa'
>>> x is y
True
>>> x = intern('a' * 21)
>>> y = intern('a' * 21)
>>> x is y
True

intern是python2的内建方法:接受一个字符串作为参数(并且只有一个字符串)。它返回一个引用——要么创建一个新的字符串,或一个已经分配好内存的字符串。“intern”保证每一个独一无二的字符串只分配一次。如果你使用“intern”第二次传入相同的字符串,Python返回一个对第一个字符串的引用。所以在需要使用重复的长字符串时,可以考虑使用intern减少内存分配。

Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(25):
...   x = 'a' * i
...   y = 'a' * i
...   print("[{}]: x is y == {}".format(i, x is y))
...
[0]: x is y == False
[1]: x is y == True
[2]: x is y == False
[3]: x is y == False
[4]: x is y == False
[5]: x is y == False
[6]: x is y == False
[7]: x is y == False
[8]: x is y == False
[9]: x is y == False
[10]: x is y == False
[11]: x is y == False
[12]: x is y == False
[13]: x is y == False
[14]: x is y == False
[15]: x is y == False
[16]: x is y == False
[17]: x is y == False
[18]: x is y == False
[19]: x is y == False
[20]: x is y == False
[21]: x is y == False
[22]: x is y == False
[23]: x is y == False
[24]: x is y == False


Python 3.4.3 (default, Mar 17 2015, 18:12:35)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(25):
...   x = 'a' * i
...   y = 'a' * i
...   print("[{}]: x is y == {}".format(i, x is y))
...
[0]: x is y == True
[1]: x is y == True
[2]: x is y == False
[3]: x is y == False
[4]: x is y == False
[5]: x is y == False
[6]: x is y == False
[7]: x is y == False
[8]: x is y == False
[9]: x is y == False
[10]: x is y == False
[11]: x is y == False
[12]: x is y == False
[13]: x is y == False
[14]: x is y == False
[15]: x is y == False
[16]: x is y == False
[17]: x is y == False
[18]: x is y == False
[19]: x is y == False
[20]: x is y == False
[21]: x is y == False
[22]: x is y == False
[23]: x is y == False
[24]: x is y == False

当整数在某个范围内时,他们使用同一个对象,超出范围就会分为两个不同对象,在同一行代码里面分配他们却使用的是同一个对象,在for循环里面都使用同一个对象

>>> x = 20
>>> y = 20
>>> x is y
True
>>> x = 20000
>>> y = 20000
>>> x is y
False
>>> x = 20000; y = 20000;
>>> x is y
True
>>> for i in range(2000):
...   x = i
...   y = i
...   if not x is y:
...     print i
...

如果您觉得从我的分享中得到了帮助,并且希望我的博客持续发展下去,请点击支付宝捐赠,谢谢!

若非特别声明,文章均为阿小信的个人笔记,转载请注明出处。文章如有侵权内容,请联系我,我会及时删除。

#Python#  
分享到:
阅读[1380] 评论[0]

你可能也感兴趣的文章推荐

本文最近访客

发表评论