Additional argument for str.join()

I’ suggesting an additional argument for str.join() so that

', '.join(['a', 'b', 'c'], ', or ')

would output

'a, b, or c' (sorry, I had an additional ‘,’ in my post at Issue 43280: additional argument for str.join() - Python tracker that was grammatically wrong and confusing)

The reason for this idea is that I often have functions where string arguments can have a finite number of different values and I’d like to have a quick way of give good feedback in ValueErrors, e.g.,

def do_something(method: str):
    methods = ['a', 'b', 'c']
    if method not in methods:
       raise ValueError(f"method is {method}, but has to be {', '.join(methods, ', or ')}") 

Might still be to specific, but I wanted to give it a shot.

Consider the case where there are two items in the list: (as a non-native speaker,) “Has to be A, or B” doesn’t feel as good as “Has to be A or B” to me. I also suppose (again, not sure about that) there might be languages where the conventions for enumerations are different. At least, both of the message string and the additional ', or ' string have to be marked as translatable in an i18n context.

Overall, this looks too specific a use case for a core API in my admittedly uneducated eye.

If there is demand for this particular style of message formatting, why not a function somewhere in the standard library, e.g., in logging?

That being said, I would personally sacrifice a bit of eloquence here and say something along the lines of:

f"Method {method} not in allowed values: {', '.join(methods)}"

I am also cautiously skeptical that this use case is frequent. Take the open() function, for example.

>>> open("file", mode="invalid")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid mode: 'invalid'

That doesn’t list allowed modes, and I think this is because there are too many for sensibly listing in an error message. help(open) explains them.

1 Like

Oh, see also this discussion: Full left and right "separator".join(L) - #3 by terrdavis

Hi Fabian,

Can you think of any use for this new feature apart from joining a
comma-separated list of words with a conjunction (“or” or “and”) at the
end?

For example, is there a use for this with some other joiner:

':'.join(words, '-')

or similar?

This seems too specific to English to be made into a general purpose
string method.