Puzzled by "is"

Steve Holden steve at holdenweb.com
Thu Aug 9 18:10:43 EDT 2007


Grzegorz Słodkowicz wrote:
>> Why? Because.
>>
>> Seriously, it's just an optimization by the implementers. There is no 
>> need for more than one empty tuple, since tuples can never be modified 
>> once created.
>>
>> But they decided not to create (1, ) in advance. They probably knew that 
>> hardly anybody would want to create that tuple ;-) [Seriously: if you 
>> started trying to predict which tuples would be used you would go 
>> insane, but the empty tuple is the most likely candidate].
>>   
> That's just theorisation but I'd rather expect the interpreter simply 
> not to create a second tuple while there already is an identical one. 
> This could save some memory if the tuple was large (Although by the same 
> token comparison of large tuples can be expensive). Admittedly the empty 
> tuple is a special case but then 'Special cases aren't special enough to 
> break the rules'.
> 
> A bit odd.
> 
It's a trade-off, preferring to optimize time rather than memory usage. 
If tuples were "interned" like some strings then tuple creation would be 
more expensive due to the need to search the cache when creating them 
which, as you rightly point out, could be very expensive for large tuples.

The integers under 100 are also, IIRC, special-cased:

 >>> x = 12
 >>> y = 32676
 >>> x is 12
True
 >>> y is 32676
False
 >>>

I guess if you *make* the rules you can decide when it's reasonable to 
break them!

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list