join does not call __str__

Roman Suzi rnd at onego.ru
Fri Jun 1 11:24:36 EDT 2001


On Fri, 1 Jun 2001, Sasa Zivkov wrote:

> >>> 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...

for some applications this is indeed convenient, but one
can easily get wanted behaviour by doing str explicitely:

"".join(map(str, b))

> Your opinion ?

So, it's not a big problem. And doesn't slow down join()


> -- Sasa

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list