Puzzled by "is"
hniksic at xemacs.org
Thu Aug 9 23:55:27 CEST 2007
Grzegorz Słodkowicz <jergosh at wp.pl> writes:
>> 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
But then tuple creation would be slowed down by searching for whether
an "identical one" already exists. In the general case, that is quite
unlikely, so it's not done. (I suspect that only the requirement to
store the list of all tuples somewhere would outweigh any potential
gains of this strategy; and if the search were implemented as a hash
table lookup, even more space would be wasted.) It's done for the
empty tuple because no search is necessary, only a size test.
> Admittedly the empty tuple is a special case but then 'Special cases
> aren't special enough to break the rules'.
Except no rule is being broken. As others have pointed out, since
tuples are immutable, caching them is quite safe.
More information about the Python-list