esoteric question about dict keys (Re: age of Python programmers)

Alexis Roda alexis.roda at urv.es
Mon Aug 23 05:48:19 EDT 2004


Steve wrote:
>>If I write my own dictionary-like
>>object, on wich the mutability of the keys is not a technical issue, is
>>considered blasphemous the use of dicts as keys? For example, querying
>>an SQL table can be partially modelled as a dictionary access:
> 

My first intention when thinking on dicts as keys was to avoid problems 
when working with tables with compound primary keys:

table[('value1', 'value2')] or it was table[('value2', 'value1')] ?

table[{'pkey1':'value1', 'pkey2':'value2'}] is less error prone since it 
states what field a value is related to

I've so obsessed with dicts that I've not realized that I can achieve 
the same with tuples:

table[(('pkey1', 'value1'), ('pkey2', 'value2'))]

although, as pointed by Adrew, a method with named parameters looks cleaner.

 > ...but the whole point of having keys is to uniquely identify a
 > record, so table[<whatever>] should always return *one* uniquely keyed
 > object.

in the case I perform an arbitrary query on the table, a tuple (of 
records) can be considered as *one* uniquely keyed object?



Thanks Steve and Andrew
-- 
                                    ////
                                   (@ @)
----------------------------oOO----(_)----OOo--------------------------
<>               Ojo por ojo y el mundo acabara ciego
/\ Alexis Roda - Universitat Rovira i Virgili - Reus, Tarragona (Spain)
-----------------------------------------------------------------------




More information about the Python-list mailing list