[Tutor] Need some clarification on this
davea at ieee.org
Sat Mar 19 19:17:48 CET 2011
On 01/-10/-28163 02:59 PM, Joel Goldstick wrote:
> 2011/3/19 Yaşar Arabacı<yasar11732 at gmail.com>
>>>>> a == b
>>>>> a is b
>> My question is, why "a is b" is true. What I expected it to be is that, a
>> and b are different things with same value.
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
> Oops. I misread your post. I (why I don't know) thought I saw a = 5, b =
> a and b are names. Python has an integer of 5 from a = 5, so it just refers
> to that same object with b.
Sometimes a particular implementation of Python will reuse the same
object, if it's immutable. So certain strings and certain integers may
be reused, presumably to save some space. But you cannot count on it.
If you tried the same thing with a value of 741, you'd probably get a
different answer. It's not time-efficient for Python to go hunting
through all the existing objects to find a match, so this optimization
is done pretty narrowly.
You seldom want to use 'is' on an integer or a string anyway. Use ==
unless it really matters, which is usually either a custom object, or a
singleton like True.
More information about the Tutor