Sets in Python

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Sep 20 09:20:27 CEST 2007


On Thu, 20 Sep 2007 03:46:08 +0000, prikar20 wrote:

> On Sep 19, 5:25 pm, Mark Dickinson <dicki... at gmail.com> wrote:
>> On Sep 19, 7:26 pm, Karthik Gurusamy <kar1... at gmail.com> wrote:
>>
>> > If I did, a = [10, 20] and I did d[a]= 'foo', then a.append(30).
>> > If dict complains key error on d[a] now, I won't be surprised. If I do
>> > d[[10, 20, 30]], I will be surprised if it doesn't find the item. Of
>> > course, in today's behavior the above is syntax error.
>>
>> It sounds as though you're proposing something like the following:
>>
>> >>> k = mylist([1, 2])
>> >>> d = {k : 'test'}
>> >>> d[k]
>> 'test'
>> >>> k.append(3)
>> >>> d[k]
>>
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> KeyError: [1, 2, 3]
>>
>> So far, so good.  But how do you explain the following to a confused
>> newcomer?
>>
>> >>> d.keys()
>> [[1, 2, 3]]
>> >>> k in d.keys()
>> True
>> >>> k in d
>> False
>> >>> d[k]
>>
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> KeyError: [1, 2, 3]
>>
>> In other words, to repeat Sion Arrowsmith's question, what would you
>> expect d.keys() to return after a key of d has been modified?
> 
> In the new model, it should be the value at the time of addition.
> That is [1,2] (not [1,2,3]). This does mean a copy of key in
> maintained internally in the dict.

A copy!?  That has to be a deep copy.  Which would make `dict`\s alot
slower and use more memory.  Plus you can't store objects that can't be
copied anymore.  That doesn't sound like a good trade off to me.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list