Python 具有变量和数据结构的身份运算符

Python Identity operators with variables and datastructures

我有以下代码:

a = []
b = a

当我编译以下代码时,我得到了:

print(b is a) --> True
print(b is []) --> False

如果 b = a 那么不应该 b is [] return True?

试试看:

    a = []
b = a


print(id(a))
print(id(b))
print(id([]))

你会看到a和b指的是同一个对象,而next[]是不同的对象。检查 if b 以查看 b 是否不为空列表