Death to tuples!

Aahz aahz at pythoncraft.com
Mon Nov 28 16:30:15 EST 2005


In article <7x64qcii9o.fsf at ruckus.brouhaha.com>,
Paul Rubin  <http://phr.cx@NOSPAM.invalid> wrote:
>skip at pobox.com writes:
>>
>> For those of us not following this thread closely, can you identify
>> cases where tuples are mutable, not hashable or can't be used as
>> dictionary keys?  I've never encountered any such cases.
>
>t = ([1,2], [3,4])

Exactly.  Technically, the tuple is still immutable, but because it
contains mutable objects, it is no longer hashable and cannot be used as
a dict key.  One of the odder bits about this usage is that augmented
assignment breaks, but not completely:

>>> t = ([1,2], [3,4])
>>> t[0] += [5]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> t
([1, 2, 5], [3, 4])

(I'm pretty sure Skip has seen this before, but I figure it's a good
reminder.)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair



More information about the Python-list mailing list