Python not a Very High-Level Language?

Tres Seaver tseaver at aftershock.neosoft.com
Mon Jan 3 13:26:25 EST 2000


In article <3870E485.998FF2FD at roguewave.com>,
bjorn  <bjorn at roguewave.com> wrote:
>
>
>Tres Seaver wrote:
>
>> In article <3870DB37.761034EA at roguewave.com>,
>> bjorn  <bjorn at roguewave.com> wrote:
<snip>
>> >So what you're saying is that dicts are broken since they can't take
>> >lists as keys <wink>.
>>
>> What reasonable use can you propose for an associative container which
>> allows mutable keys?  You might as well revert to a list of (key,value)
>> pairs, since you're going to do a linear search on each lookup anyway
>> if the keys are allowed to change.
>
>If you look at any hashtable implementation for C, you'll see that the keys
>can >be char* (or any other mutable type for that matter).  There are really
>only two >requirements for the keys in an associative container (i) they must
>be hashable, and (ii) they must remain unchanged for the duration of the
>container (or more >specifically, the hash value must remain unchanged).

Strings are immutable in Python, and thus make ideal keys.  

>It's might be nice (for a suitable definition of nice) to have the language
>hold your hand through this, but it is far from neccessary.  Sometimes it
>really does suffice to tell the programmer that if he changes a key he'll
>get undefined behavior.

And indeed, constructing a tuple of mutable elements can generate just such
behavior, even in Python (although it requires some trickery to make it happen,
as Python knows about the obvious cases, and complains of "unhashable type").

However, dictionaries, and the proper working thereof, are essential to
Python's own implementation -- they do not exist merely as "user-space" data
structures.  Easing unsafe practices here is asking for trouble.

Corned-beef-or-turkey-hash?'ly

Tres.
-- 
---------------------------------------------------------------
Tres Seaver           tseaver at palladion.com       713-523-6582
Palladion Software    http://www.palladion.com



More information about the Python-list mailing list