[Tutor] Getting at object names

Charlie Clark charlie@begeistert.org
Wed Apr 2 16:55:02 2003


Dear all,

thanx very much for the explanations. Alan helped me understand _why_ I 
couldn't do what I wanted:

> No, because its just a symbol in the program (OK you could read the 
> local namespace dictionary but that's messy and not to be encouraged! 
> Why not just associate the dayname with the key?

and came up with practicable solution

days = {1:('friday',friday),2:('saturday',saturday).....}

for day in d
   print days[day][0],days[day][1]


Jeff's solution but seems slightly more elegant and less subject to typos 
(I make lots)

days = {'1': ('friday', []), '2': ('saturday', []), '3': ('sunday', [])}

d = days.keys()
d.sort()
for day in d:
    name, values = days[d]
    print '%s : %s' % (name, values)

And Bob came up with a really nice solution which I'm afraid I can't use 
because I'm currently running Python 2.1.2 on BeOS for development so I'll 
be going with Jeff's solution for the time being.

#subclass list and give it a name attribute:
class day(list):
   def __init__(self, name):
     self.name = name
   def set(self, items):
     self[:] = items

Thanx again to all!

Charlie