Make str.join convert non-str objects implicitly

While I understand the mantra of “Explicit is better than implicit.” in most cases, I wonder what could possibly be a downside to making str conversions implicit for the str.join method.

Wouldn’t this be more intuitive and readable:

' '.join([1, 2, 3])

than the currently accepted boilerplate of mapping items to str first before joining them into a string:

' '.join(map(str, [1, 2, 3]))

I mean, the current code already spends the CPU time to validate that each item is a string, so why not do the helpful thing of converting the item to a string instead of complaining about it, since the intention is always to join the items as a string?

1 Like

Should str.join use an object’s __str__ or its __repr__?

There’s no definite answer for what the would user want. It’s better to let them explicitly decide how they want their objects to be stringified.

Another part of the Zen comes to mind:

In the face of ambiguity, refuse the temptation to guess.

Then why does print call str implicitly for non-str objects, relying on the implicit behavior of str calling __repr__ if __str__ doesn’t exist?

As long as the rules are documented and reasonably intuitive, I don’t see why not.

1 Like

Previous discussions:

1 Like

Closing as this has already been thoroughly discussed.