Glauco, <br>
<br>
Be careful if you decide to use hash. <br>
There is possibility of bugs due to that approach, (if hash(x) == hash(y) and x != y).<br>
Even if the probability of bug is near 0, your computer will certainly recall you what is the murphy law.<br><br>
If I were you, I would prefer another approach. <br>
<br>
Cyril<br>
<br><div><span class="gmail_quote">On 7/19/05, <b class="gmail_sendername">Glauco</b> <<a href="mailto:00515879256@fastwebnet.it">00515879256@fastwebnet.it</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Steven D'Aprano wrote:<br>> On Mon, 18 Jul 2005 12:17:37 +0200, Glauco wrote:<br>><br>><br>>>I want to insert a concept of alias in a dict_based class.<br>>><br>>>The idea  is to have a facoltative name in the same dict that correspond
<br>>>at the same value. With this alias i can change original value.<br>>><br>>>example:<br>>><br>>>mydict['a'] = 1<br>>>I must define an alias  example: myFunctAlias( mydict, 'a', 'b')
<br>>>print mydict<br>>>{'a':1, 'b':1}<br>>>mydict['b'] = 2<br>>>print mydict<br>>>{'a':2, 'b':2}<br>>><br>>><br>>>The only idea i have is to implement two dictionary one for convert
<br>>>name, alias in two keys with the same value (eg.numeric) in the first<br>>>dict. The second for store only one time the k, v .<br>><br>><br>> You need some sort of redirection, something like this (untested):
<br>><br>> class Doubledict:<br>>     def __init__(self, **kargs):<br>>         self.data = {}<br>>         self.aliases = {}<br>>         for key in kargs:<br>>             # Point the key to a hash.
<br>>             self.aliases[key] = hash(key)<br>>             # And point the hash at the value.<br>>             self.data[hash(key)] = kargs[key]<br>><br>>     def setalias(self, key, alias):<br>>         # Point the alias to the same hash as the real key.
<br>>         self.aliases[alias] = hash(key)<br>><br>>     def __getitem__(self, key):<br>>         return self.data[self.aliases[key]]<br>><br>>     def __setitem__(self, key, value):<br>>         self.data
[self.aliases[key]] = value<br>><br>><br>> The only niggly worry I have is I'm not sure when hash can be used, when<br>> it is unique, or even if is it guaranteed to be unique.<br>><br><br>Thank Steve, the idea was the same...
<br>but yours using hash is much elegant.<br><br>Thank you<br>Glauco<br><br>--<br><br>                          \\\|///<br>                        \\  -
-  //<br>                        
(  @ @  )<br>+---------------------oOOo-( )-oOOo--------------------------+<br>|                                                            |<br>| I have a dream that one day this nation will rise up and   |<br>|   live out the true meaning of its creed: "We hold these   |
<br>|   truths to be self-evident:that all men are created equal.|<br>| I have a dream that one day on the red hills of Georgia    |<br>|   the sons of former slaves and the sons of former         |<br>|   slaveowners will be able to sit down together at a table |
<br>|  
of
brotherhood.                                          |<br>| I have a dream that one day even the state of Mississippi, |<br>|   a desert state, sweltering with the heat of injustice    |<br>|   and oppression, will be transformed into an oasis of     |
<br>|  
freedom and
justice.                                    
|<br>| I have a dream that my four children will one day live in  |<br>|   a nation where they will not be judged by the color of   |<br>|   their skin but by the content of their character.        |<br>|
I have a dream
today.                                      |<br>|                                                            |<br>|                        Martin
Luther King, Jr  28 Ago 1963 |<br>+------------------------------------------------------------+<br>|                    glauco(at)uriland.it                    |<br>|  <a href="http://www.uriland.it">www.uriland.it</a>      .oooO                ICQ:
115323690   |<br>+--------------------- (   )------ Oooo.---------------------+<br>                        
\ (        (   )<br>                          \_)        )
/<br>                                    (_/<br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br>