print names of dictionaries

B

BartlebyScrivener

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
 
T

Thomas Nelson

Here's an OO way that may do what you want:.... def __init__(self,dic,rep):
.... dict.__init__(self,dic)
.... self.rep = rep
.... def __repr__(self):
.... return self.rep
....['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
 
B

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
 
B

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

Thank you both for your help.

rpd
 
T

Thomas Nelson

Here's an OO way that may do what you want:.... def __init__(self,dic,rep):
.... dict.__init__(self,dic)
.... self.rep = rep
.... def __repr__(self):
.... return self.rep
....['alpha', 'beta']

Of course, the easiest way is just to use a tuple (dict,string).

THN
 
B

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
 
S

Scott David Daniels

BartlebyScrivener said:
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
(e-mail address removed)
 
T

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, ":",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
 
B

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
 
P

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 = id(p)
return p

#ex
object = create_object ([1,2,3,4], 'A LIST')

Philippe





Thomas said:
Here's an OO way that may do what you want:... def __init__(self,dic,rep):
... dict.__init__(self,dic)
... self.rep = rep
... def __repr__(self):
... return self.rep
...['alpha', 'beta']

Of course, the easiest way is just to use a tuple (dict,string).

THN
 
P

Philippe Martin

OK, totally dumb !

g_dict = p




Philippe said:
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 = id(p)
return p

#ex
object = create_object ([1,2,3,4], 'A LIST')

Philippe





Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top