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

Nick Coghlan ncoghlan at gmail.com
Mon Feb 28 23:09:19 CET 2011


On Tue, Mar 1, 2011 at 3:35 AM, Guido van Rossum <guido at python.org> wrote:
> It comes up occasionally and is aways rejected on the same grounds as
> rejecting '1' + 2. I.e., we believe that the current approach catches
> more bugs.

With the advent of bytes.join, this is even more true than ever:

>>> data = b'1', b'2', b'3', b'4'
>>> data
(b'1', b'2', b'3', b'4')
>>> b''.join(data) # Intended operation
b'1234'
>>> ''.join(data) # Lack of coercion means a typo gives an immediate error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected str instance, bytes found
>>> ''.join(map(str, data)) # Implicit coercion would convert exception into bad output
"b'1'b'2'b'3'b'4'"

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list