Nant
Nant

Reputation: 569

Python Identity operators with variables and datastructures

I have the following code:

a = []
b = a

when I compile the following code I get this:

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

if b = a then shouldn't b is [] return True?

Upvotes: 1

Views: 97

Answers (1)

snamef
snamef

Reputation: 195

try that:

    a = []
b = a


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

And you will see that a and b refers to the same object, while next [] is a different one. Check if b to see if b is not empty list

Upvotes: 5

Related Questions