type(a)== type(b)
与 type(a) is type(b)
的区别,为什么用后者不用前者呢?
type(a)== type(b)
与 type(a) is type(b)
的区别,为什么用后者不用前者呢?
type(a) == type(b)
: a,b继承的类 (类也是基类的实例) 值相等 就像:
c = [1,2,3]
d = [1,2,3]
c == d
>>> True
c is d
>>> false
type(a) == type(b)
: a,b继承的类 是同一个实例(内存地址相同)就像
c = 1
d = 1
c == d
>>> True
c is d
>>> True
也举个例子
class A(object):
def __eq__(self, other):
return False
a = A()
print(a == a) #False
print(a is a) #True
4 回答4.4k 阅读✓ 已解决
1 回答3.1k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
1 回答4.4k 阅读✓ 已解决
1 回答3.9k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决
is
check 两边的值是否为同一对象.==
实际上call
了左值的__eq__()
, 然后pass给右值.