print names of dictionaries

Philippe Martin pmartin at snakecard.com
Thu Apr 27 08:58:49 EDT 2006


OK, totally dumb !

g_dict[s] = p




Philippe Martin wrote:

> Hi,
> 
> I do not know if there is a way to overload the instantiation of all
> objects in Python but I thought of something like this to fetch any object
> with its name:
> 
> g_dict = {}
> 
> 
> def create_object (v,s):
>    p = v
>    g_dict[s] = id(p)
>    return p
> 
> #ex
> object = create_object ([1,2,3,4], 'A LIST')
> 
> Philippe
> 
> 
> 
> 
> 
> Thomas Nelson wrote:
> 
>> Here's an OO way that may do what you want:
>>>>> class MyD(dict):
>> ...     def __init__(self,dic,rep):
>> ...             dict.__init__(self,dic)
>> ...             self.rep = rep
>> ...     def __repr__(self):
>> ...             return self.rep
>> ...
>>>>> apps = MyD({'alpha':1,'beta':2},'apps')
>>>>> apps
>> apps
>>>>> apps.keys()
>> ['alpha', 'beta']
>> 
>> Of course, the easiest way is just to use a tuple (dict,string).
>> 
>> THN




More information about the Python-list mailing list