Python复习笔记——is

在网上搜了一下,不少人发帖问python的is==这两个比较操做符的区别,关于这个,官方文档有一些说明。python

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value.ide

上面说明了is操做符用来判断两个操做数是否是同一个对象,也就是它们引用的是否是同一个对象。
不过在下面注释那里又指出,code

Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info.对象

由于自动GC,涉及到实例方法或者常量的比较的时候,你可能会看到一些不一样寻常的现象。
这里的常量应该是说这种状况ip

>>> x=1
>>> y=1
>>> x is y
True
>>> id(x)
140504559802792
>>> id(y)
140504559802792

这里之因此,x is yTrue多是为了减小内存分配,采用了相似了C的作法,把常量数据放在一个固定的区域,而后若是后面有使用相应的常量,则直接引用。
至于实例方法,等找个例子才写。内存

相关文章
相关标签/搜索