tuples vs lists
Antoon Pardon
apardon at forel.vub.ac.be
Wed Jan 12 03:33:04 EST 2005
Op 2005-01-11, Reinhold Birkenfeld schreef <reinhold-birkenfeld-nospam at wolke7.net>:
> Antoon Pardon wrote:
>> Op 2005-01-10, Bruno Desthuilliers schreef <bdesth.quelquechose at free.quelquepart.fr>:
>>> Antoon Pardon a écrit :
>>>> Op 2005-01-08, Bruno Desthuilliers schreef <bdesth.quelquechose at free.quelquepart.fr>:
>>>>
>>>>>worzel a écrit :
>>>>>
>>>>>>I get what the difference is between a tuple and a list, but why would I
>>>>>>ever care about the tuple's immuutability?
>>>>>
>>>>>Because, from a purely pratical POV, only an immutable object can be
>>>>>used as kay in a dict.
>>>
>>><my-bad> s/kay/key/ </my-bad>
>>>
>>>> This is not true.
>>>
>>> Chapter and verse, please ?
>>
>> I don't need chapter and verse. I have already used mutable
>> objects as keys and it works just fine.
>>
>>>>> class hlst(list):
>>>>>
>>>>> def __hash__(self):
>>>>> sum = 0
>>>>> for el in self:
>>>>> sum += hash(el)
>>>>> return sum % 0x37777777
>>>>>
>
> Given this hash function, how do you handle changed keys?
I don't change keys. The fact that I like to use a mutable
as a key doesn't imply I want to mutate a key.
> And if you can't access the element when it's changed, what is the
> advantage over using tuples?
The debate over what the adavantage is of tuples over lists or vice
versa as keys in dictionaries is IMO misguided. Whether I use a list
or a tuple is not guided by whether they are going to be used as a
key or not, but how in general the data is to be manipulated.
If the typical manipulations are modifications of an existing object,
I use a list, if the typical manipulation creates new objects out
of old ones I use a tuple. If I then find that I need this object
as a key, I just provide a hash so that I can use this object as
a key in a straight forward manner, without the hassle of converting
to and from a tuple all the time.
--
Antoon Pardon
More information about the Python-list
mailing list