python中is和== 的差別
is 是 如果兩個變數都指向同一個物件 就會return
True
>>> 1000 is 10**3
False
>>> 1000 == 10**3
True
== 是 物件推斷變數為同一個
>>> "a" is "a"
True
>>> "aa" is "a" * 2
True
>>> x = "a"
>>> "aa" is x * 2
False
>>> "aa" is intern(x*2)
True
True
>>> 1000 is 10**3
False
>>> 1000 == 10**3
True
>>> "a" is "a"
True
>>> "aa" is "a" * 2
True
>>> x = "a"
>>> "aa" is x * 2
False
>>> "aa" is intern(x*2)
True
留言
張貼留言