码迷,mamicode.com
首页 > 编程语言 > 详细

Is there a difference between `==` and `is` in Python?

时间:2016-12-27 13:38:40      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:value   like   reference   pytho   com   tac   als   class   one   

There is a simple rule of thumb to tell you when to use == or is.

  • == is for value equality. Use it when you would like to know if two objects have the same value.
  • is is for reference equality. Use it when you would like to know if two references refer to the same object.
>>> a = 500
>>> b = 500
>>> a == b
True
>>> a is b
False

 

 

注: 判断None的只能用xxx is None来做

 

ref: http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python

https://segmentfault.com/q/1010000000150947

Is there a difference between `==` and `is` in Python?

标签:value   like   reference   pytho   com   tac   als   class   one   

原文地址:http://www.cnblogs.com/pinganzi/p/6225423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!