Re: print names of dictionaries

2006-04-27 Thread Philippe Martin
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 >

Re: print names of dictionaries

2006-04-27 Thread Philippe Martin
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') Ph

Re: print names of dictionaries

2006-04-26 Thread BartlebyScrivener
Thomas, Thanks. I read about tuple packing and unpacking. Now I get to see it in action. Plus, yours is one line shorter. If programming is anything like writing, shorter is almost always better. Thanks, rick -- http://mail.python.org/mailman/listinfo/python-list

Re: print names of dictionaries

2006-04-26 Thread Thomas Nelson
I meant something like def printdict(dictionaries=[(apps,'apps'), (dirs,'dirs'), (sites,'sites')]): for dictionary,name in dictionaries: print name keys = dictionary.keys() keys.sort() for key in keys: print key, ":

Re: print names of dictionaries

2006-04-26 Thread Scott David Daniels
BartlebyScrivener wrote: > This works for now. I just added their names as values: > > def printdict(dictionaries=[apps, dirs, sites]): >for dictionary in dictionaries: > print dictionary["name"] > keys = dictionary.keys() > keys.sort() >

Re: print names of dictionaries

2006-04-26 Thread BartlebyScrivener
>> Of course, the easiest way is just to use a tuple (dict,string). I don't mean to be obtuse, but I'm not getting this one either. Is it easier than what I did? Thx, rick -- http://mail.python.org/mailman/listinfo/python-list

Re: print names of dictionaries

2006-04-26 Thread Thomas Nelson
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 >>> a

Re: print names of dictionaries

2006-04-26 Thread BartlebyScrivener
Yikes, I'll have to come back to the OO way in a month or two ;) This works for now. I just added their names as values: def printdict(dictionaries=[apps, dirs, sites]): for dictionary in dictionaries: print dictionary["name"] keys = dictionary.keys()

Re: print names of dictionaries

2006-04-26 Thread BartlebyScrivener
Wow, That's food for thought. Thanks. I see what they mean about change of approach. I'll just stick a key in each dictionary called, er, name with its name value. Thank you! rick -- http://mail.python.org/mailman/listinfo/python-list

Re: print names of dictionaries

2006-04-26 Thread Thomas Nelson
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 >>> a

Re: print names of dictionaries

2006-04-26 Thread jay graves
BartlebyScrivener wrote: > Still new. Learning attributes and functions and so on. > Sorry if this is obvious, but if I'm defining a function for some > dictionaries, how can I print just the names of the dictionaries? Short answer: You can't. http://pyfaq.infogami.com/how-can-my-code-discover-t