0

is operator in Python

Example: a=1 print(a is 1)//True a=[2,3,4] print(a is [2,3,4])//False The problem is that I think a and 1 is not identical. But it seems that Python creates just one immutable variable in a program, but I think there is no limit for creating mutables, am I right? Thanks.

23rd Feb 2019, 3:18 PM
Yusuf
Yusuf - avatar
1 Odpowiedź
+ 4
An implementation of Python has the freedom to let several immutable equal objects be just one object; but for mutable objects that is forbidden, they have to be separate objects. Unless you explicitly (or maybe more often even mistakenly ^^) say, two different names should be one object, like: a = [1, 2] b = a With an assignment, you basically just strip the name sticker off an object and stick it somewhere different. So with immutables, since you have no methods to change them, it doesn't matter. When an object has methods to change, though, haphazardly melding them into one object can destroy data.
23rd Feb 2019, 3:38 PM
HonFu
HonFu - avatar