Problem with join in__str__() in class (newbie)

Fencer no.i.dont at want.mail.from.spammers.com
Sun Aug 9 13:16:50 EDT 2009


Hello, I've written two classes. One class describes experts: experts 
has a unique ID and a name. An expert knows topics and other experts. A 
topic is described by my other class and includes a unique ID and a 
name. Now I have a problem with the __str__ method in my Expert class:

def __str__(self):
     output = '%s:%s' % (self.expert_id, self.name)
     output += '\nKnown topics: %s' % (', '.join(str(self.topics)))
#   print 'Known experts: '
#   for e in self.known_experts:
#       print '%s:%s' % (e.expert_id, e.name)
     return output

self.topics is a list of objects of type Topic.
self.known_experts is a list of objects of type Expert, specifically the 
experts known by the given expert.

When I print an object of type Expert, the output is not what I want. If 
the expert knows only one topic, say polemics, the output is:
e2:Carla
Known topics: t, 5, :, P, o, l, e, m, i, c, s
If the expert knows two topics, say Polemics and The Parthenon, then the 
  output is:
e2:Carla
Known topics: [, <, _, _, m, a, i, n, _, _, ., T, o, p, i, c,  , i, n, 
s, t, a, n, c, e,  , a, t,  , 0, x, 0, 2, 2, 2, D, 0, 8, 0, >, ]

This is not what I want. :) I want the output like this:
e2:Carla
Known topics: t5:Polemics, t6:The Parthenon

Also, notice the code I've commented out. If I can get the join above to 
work (with your help) my next question is how to present the known 
experts in a comma separated list with only expert_id and name? I can't 
use the normal __str__() method (the one I'm writing here) because it 
prints too much information. Does that mean a join is out of the question?

Thanks for any replies!

- Fencer



More information about the Python-list mailing list