join does not call __str__

Andreas Jung andreas at digicool.com
Fri Jun 1 11:43:38 EDT 2001


Not really. The string.join() documentation says that is joins a sequence of
words (strings). There is no implicit conversion of the arguments using
str().
Use instead:

   b=['c','d',str(a)]

Andreas
----- Original Message -----
From: "Sasa Zivkov" <sasha at spuni.is>
To: <python-list at python.org>
Sent: Friday, June 01, 2001 11:12 AM
Subject: join does not call __str__


>
> >>> class A:
> ... def __str__(self):
> ... return "str-a"
> ...
> >>> a = A()
> >>> print a
> str-a
> >>> b = ['c', 'd', a]
> >>> "".join(b)
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: sequence item 2: expected string, instance found
>
>
> Somehow I expected that join method would use __str__ method to convert
> instance to string...
>
> Your opinion ?
>
> -- Sasa
>
> --
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list