On 15 September 2017 at 03:38, Antoine Rozo email@example.com wrote:
Why is it ','.join(iterable), why isn't there join(',', iterable)
Because join apply on a string, and strings are defined by the str class, not by a specific protocol (unlike iterables).
Join actually used to only be available as a function (string.join in Python 2.x). However, nobody could ever remember whether the parameter order was "join this series of strings with this separator" or "use this separator to join this series of strings" - it didn't have the same kind of natural ordering that map and filter did thanks to the old "apply" builtin ("apply this function to these arguments" - while the apply builtin itself is no longer around, the "callable(*args, **kwds)" ordering that corresponds to the map() and filter() argument ordering lives on).
This meant that when string.join became a builtin interface, it became a string method since:
1. Strings are one of the few Python APIs that *aren't* protocol centric - they're so heavily intertwined with the interpreter implementation, that most folks don't even try to emulate or even subclass them*. 2. As a string method, it's obvious what the right order has to be (separator first, since it's the separator that has the method)
As a result, the non-obvious to learn, but easy to remember, method spelling became the only spelling when string.join was dropped for Python 3.0.
* One case where it was actually fairly common for folks to define their own str subclasses, rich filesystem path objects, finally gained its own protocol in Python 3.6