Dictionary, keys and alias

Cyril Bazin cyril.bazin at gmail.com
Tue Jul 19 10:42:07 EDT 2005


Glauco, 

Be careful if you decide to use hash. 
There is possibility of bugs due to that approach, (if hash(x) == hash(y) 
and x != y).
Even if the probability of bug is near 0, your computer will certainly 
recall you what is the murphy law.

If I were you, I would prefer another approach. 

Cyril

On 7/19/05, Glauco <00515879256 at fastwebnet.it> wrote:
> 
> Steven D'Aprano wrote:
> > On Mon, 18 Jul 2005 12:17:37 +0200, Glauco wrote:
> >
> >
> >>I want to insert a concept of alias in a dict_based class.
> >>
> >>The idea is to have a facoltative name in the same dict that correspond
> >>at the same value. With this alias i can change original value.
> >>
> >>example:
> >>
> >>mydict['a'] = 1
> >>I must define an alias example: myFunctAlias( mydict, 'a', 'b')
> >>print mydict
> >>{'a':1, 'b':1}
> >>mydict['b'] = 2
> >>print mydict
> >>{'a':2, 'b':2}
> >>
> >>
> >>The only idea i have is to implement two dictionary one for convert
> >>name, alias in two keys with the same value (eg.numeric) in the first
> >>dict. The second for store only one time the k, v .
> >
> >
> > You need some sort of redirection, something like this (untested):
> >
> > class Doubledict:
> > def __init__(self, **kargs):
> > self.data = {}
> > self.aliases = {}
> > for key in kargs:
> > # Point the key to a hash.
> > self.aliases[key] = hash(key)
> > # And point the hash at the value.
> > self.data[hash(key)] = kargs[key]
> >
> > def setalias(self, key, alias):
> > # Point the alias to the same hash as the real key.
> > self.aliases[alias] = hash(key)
> >
> > def __getitem__(self, key):
> > return self.data[self.aliases[key]]
> >
> > def __setitem__(self, key, value):
> > self.data[self.aliases[key]] = value
> >
> >
> > The only niggly worry I have is I'm not sure when hash can be used, when
> > it is unique, or even if is it guaranteed to be unique.
> >
> 
> Thank Steve, the idea was the same...
> but yours using hash is much elegant.
> 
> Thank you
> Glauco
> 
> --
> 
> \\\|///
> \\ - - //
> ( @ @ )
> +---------------------oOOo-( )-oOOo--------------------------+
> | |
> | I have a dream that one day this nation will rise up and |
> | live out the true meaning of its creed: "We hold these |
> | truths to be self-evident:that all men are created equal.|
> | I have a dream that one day on the red hills of Georgia |
> | the sons of former slaves and the sons of former |
> | slaveowners will be able to sit down together at a table |
> | of brotherhood. |
> | I have a dream that one day even the state of Mississippi, |
> | a desert state, sweltering with the heat of injustice |
> | and oppression, will be transformed into an oasis of |
> | freedom and justice. |
> | I have a dream that my four children will one day live in |
> | a nation where they will not be judged by the color of |
> | their skin but by the content of their character. |
> | I have a dream today. |
> | |
> | Martin Luther King, Jr 28 Ago 1963 |
> +------------------------------------------------------------+
> | glauco(at)uriland.it |
> | www.uriland.it <http://www.uriland.it> .oooO ICQ: 115323690 |
> +--------------------- ( )------ Oooo.---------------------+
> \ ( ( )
> \_) ) /
> (_/
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050719/d361489a/attachment.html>


More information about the Python-list mailing list