is VS ==

2018-08-06  本文已影响5人  好小葱1

参考地址:https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python
https://www.toutiao.com/a6586443472520086023/?tt_from=weixin&utm_campaign=client_share&wxshare_count=1&timestamp=1533532946&app=news_article&utm_source=weixin&iid=39286830241&utm_medium=toutiao_android&group_id=6586443472520086023

is will return True if two variables point to the same object(对象标识符), == if the objects referred to by the variables are equal.
用来检查对象的标示符是否一致,也就是比较两个对象在内存中的地址是否一样,而 == 是用来检查两个对象是否相等。
检查a is b的时候,其实相当于检查id(a) == id(b)。而检查 a == b 的时候,实际是调用了对象 a__eq()__方法,a == b 相当于 a.__eq__(b)

image.png
上一篇下一篇

猜你喜欢

热点阅读