print names of dictionaries

Thomas Nelson thn at mail.utexas.edu
Wed Apr 26 17:32:20 EDT 2006


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).

As a side note, since dict is a builtin type and function, it might not
be good style to use it as a loop variable.

THN




More information about the Python-list mailing list