【6.2】==和is的区别

 1 #!/user/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 a = [1, 2, 3, 4]
 5 b = [1, 2, 3, 4]
 6 print(id(a), id(b))
 7 print(a is b)
 8 
 9 # 对于1或者‘abc’,python会创建一个全局唯一的一个对象
10 c = 1
11 d = 1
12 e = 'abc'
13 f = 'abc'
14 print(id(c), id(d))
15 print(id(e), id(f))
16 print(c is d)
17 print(e is f)
18 
19 
20 class People:
21     pass
22 
23 
24 person = People()
25 if type(person) is People:
26     print('yes')
2341737489032 2341737489096
False
140734558986896 140734558986896
2341737916208 2341737916208
True
True
yes

  ==比较的是值,is比较的是内存地址

原文地址:https://www.cnblogs.com/zydeboke/p/11254848.html