470,613 Members | 2,153 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

print names of dictionaries

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?

E.g. assuming apps, dirs, sites are dictionaries defined in the module,
how can I print just their names before doing other things with them.

def printdict(dicts=[apps, dirs, sites]):
for dict in dicts:
print ???

Thank you

rpd

Apr 26 '06 #1
11 1185
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...e-of-an-object

....
Jay Graves

Apr 26 '06 #2
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

Apr 26 '06 #3
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

Apr 26 '06 #4
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()
keys.sort()
for key in keys:
if key != "name":
print key, ":",dictionary[key]
print '\n',

Thank you both for your help.

rpd

Apr 26 '06 #5
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

Apr 26 '06 #6
>> 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

Apr 26 '06 #7
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()
for key in keys:
if key != "name":
print key, ":",dictionary[key]
print '\n',

Thank you both for your help.


You might want to use a key of '_name', or '_name_', because
'name' is a fairly likely name to encounter.

--Scott David Daniels
sc***********@acm.org
Apr 26 '06 #8
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, ":",dictionary[key]
print '\n',

It's not really easier than what you did. Instead of making sure every
dictionary in the dictionaries argument contains a 'name' key that does
what you expect, you must make sure the argument passed in is a list of
(dictionary, name) pairs. It's a little better in my personal opinion,
because you don't have to modify the dictionaries themselves, and it
avoids the problem of 'name' already existing in the dictionary, as
described by Scott Daniels. But I suppose that's only one opinion.

THN

Apr 27 '06 #9
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

Apr 27 '06 #10
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


Apr 27 '06 #11
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


Apr 27 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Till Plewe | last post: by
23 posts views Thread by stewart.midwinter | last post: by
210 posts views Thread by Christoph Zwerschke | last post: by
4 posts views Thread by Livin | last post: by
8 posts views Thread by placid | last post: by
69 posts views Thread by Edward K Ream | last post: by
1 post views Thread by beef | last post: by
1 post views Thread by kirby.urner | last post: by
14 posts views Thread by cnb | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.