[Python-ideas] Coercing str.join() argument elements to str

Dirkjan Ochtman dirkjan at ochtman.nl
Mon Feb 28 18:27:20 CET 2011


I just saw someone mention this on Twitter, and I know that I've been
bitten by it before. Seems like it wouldn't break any existing code...
The worst that could happen is that someone gets nonsensical strings
in new code instead of an exception.

>>> ', '.join(range(5))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> ', '.join(str(i) for i in range(5))
'0, 1, 2, 3, 4'

Has this been discussed before? If not, what would be reasons not to do this?

Cheers,

Dirkjan



More information about the Python-ideas mailing list