Coercing str.join() argument elements to str
![](https://secure.gravatar.com/avatar/1b171ce1f20ce82a31999e843a7fe9b6.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/415203f2727ceaf56d8f7f5e6d5d508b.jpg?s=120&d=mm&r=g)
![](https://secure.gravatar.com/avatar/047f2332cde3730f1ed661eebb0c5686.jpg?s=120&d=mm&r=g)
On Mon, Feb 28, 2011 at 9:27 AM, Dirkjan Ochtman <dirkjan@ochtman.nl> wrote:
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?
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. -- --Guido van Rossum (python.org/~guido)
![](https://secure.gravatar.com/avatar/f3ba3ecffd20251d73749afbfa636786.jpg?s=120&d=mm&r=g)
On Tue, Mar 1, 2011 at 3:35 AM, Guido van Rossum <guido@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@gmail.com | Brisbane, Australia
![](https://secure.gravatar.com/avatar/5615a372d9866f203a22b2c437527bbb.jpg?s=120&d=mm&r=g)
Dirkjan Ochtman wrote:
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.
"I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More experienced programmers realize that correct code is great, code that crashes could use improvement, but incorrect code that doesn’t crash is a horrible nightmare." -- Chris Smith http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ -- Steven
participants (5)
-
Dirkjan Ochtman
-
Guido van Rossum
-
Laurens Van Houtven
-
Nick Coghlan
-
Steven D'Aprano