Effects of caching frequently used objects, was Re: Explaining names vs variables in Python

Ethan Furman ethan at stoneleaf.us
Fri Mar 25 12:45:10 EDT 2016


On 03/25/2016 06:03 AM, Albert-Jan Roskam wrote:
 > Somebody wrote:
 >> Somebody else wrote:

>>> I know Python does not have variables, but names.
>>> Multiple names cant then be bound to the same objects.
>>>
>>> So this behavior
>>>
>>>--> b = 234
>>>--> v = 234
>>>--> b is v
>>> True
>>>
>>> according to the above that is ok
>>>
>>>
>>>
>>> But where is the consistency ? if I try :
>>>
>>>--> v = 890
>>>--> w = 890
>>>--> v is w
>>> False
>>>
>>> It is a little difficult to explain this behavior to a newcommer in Python
>>>
>>> Can someone give me the right argument to expose ?
>>
>> You should not bother with object identity for objects other than None.

No.  The correct answer is: if identity is important either ensure the 
object you are getting back is a singleton (such as None, True, an Enum 
member, etc.) or you assign one name from another name:

--> b = 234
--> v = b
--> b is v
True
--> v = 890
--> w = v
--> v is w
True

If identity is not important, don't use `is`.

> A little late to the party, but: how about Ellipsis? Shouldn't "is" also be used for that one? (It's rare, I know :))

Ellipsis is a singleton, so `is` is fine.

--
~Ethan~



More information about the Python-list mailing list