Problem with join in__str__() in class (newbie)

Stephen Hansen apt.shansen at
Sun Aug 9 20:06:44 CEST 2009

> only the first topic is printed. If topics only contain a single topic I
> get this error:
> Traceback (most recent call last):
>  File "C:\Users\fencer\workspace\Find Expert\src\", line 57,
> in <module>
>    print experts[1]
>  File "C:\Users\fencer\workspace\Find Expert\src\", line 21,
> in __str__
>    output += '\nKnown topics: %s' % (', '.join([str(x) for x in
> self.topics]))
> TypeError: iteration over non-sequence
> What did I do wrong?

It looks like, "if topics contains only a single topic", means:

    self.topics = MyTopic

was done at some point. What you should do is make self.topics ALWAYS be a
list: even if it's only a list of one item.

So you do


when you add that one topic. That way self.topics is always a list.
Sometimes empty, sometimes containing one element, sometimes containing

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list