Allow list of integers in `str.join`

It is not clear to me. Do you want the str(), repr(), ascii() or some other string conversion of the objects? Why or why not?

As I wrote here some weeks back, we need to distinguish between functions which are part of a low-level API, and those expected to work as part of a higher level API.

print has a high-level API. It should be polymorphic, and work with any type. I should be able to print any object at all, and get something sensible, without caring too much about it. It’s okay for print to guess what converter we want.

Because printing is a high-level API, I’m unlikely to capture the output of print and use it in other computations, so “something sensible” doesn’t need to be too precise. print is not a building block to create complex tools, it is one of those complex tools.

str.join is part of a low-level string API, which is why it shouldn’t try to guess what the user wants to do with non-string values:

  • is it an error? if so, raise
  • or did the programmer intend there to be a non-string in the input?
  • if so, how does the programmer want to convert the value into a string?

A low-level API should not guess what is wanted. In this case, explicit is better than implicit:

sep.join(map(ascii, values))

If you want a high-level joiner that works on anything, like print, it is a one-liner:

def join(values, *, sep=''):
    return sep.join([str(obj) for obj in values])

But it hardly seems worth it, for such a simple operation.

1 Like