pythonidentity-operator

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?


Solution

  • 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