Problem with join in__str__() in class (newbie)

Stephen Hansen apt.shansen at gmail.com
Sun Aug 9 14:06:44 EDT 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\find_expert.py", line 57,
> in <module>
>    print experts[1]
>  File "C:\Users\fencer\workspace\Find Expert\src\find_expert.py", 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

    self.topics.append(MyTopic)

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

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090809/cbc22105/attachment-0001.html>


More information about the Python-list mailing list