7 ms·
>Python is really consistent when it comes to its behaviour True, though you end up with things like: ' '.join(thelist) Instead of thelist.join(' ') Be
by tyingq 5y ago
>Python is really consistent when it comes to its behaviour
True, though you end up with things like:
' '.join(thelist)
Instead of
thelist.join(' ')
Because of the somewhat aggressive mantra to be consistent.
- Marazan 5y agoBut that's good. Because a string just needs to know aboit interable to perform that operation whereas every iterable would need to implement it's own join if you had it the other way around.
- goatlover 5y agoYet Ruby and JS manage to do it somehow. To me it seems natural that join should be a method on the iterable, and I always have to pause to remember Python is different.
- aidos 5y agoHow does that work? Don’t you have to effectively convert your general iterable to an array and then join on that? Array.from(iterable).join(…)?
- deleted 5y ago[deleted]
- globular-toast 5y agoI don't think it should be a method at all. It's just a function: join(iterable, separator). It can also be implemented with reduce naturally: `reduce(lambda x, y: x + separator + y, iterable)`.
- Redoubts 5y agoReduce sounds like a really slow way to do string building
- globular-toast 5y agoOh yeah, it's horrendous, my point was just that it's functionally equivalent and makes more sense as a function than a method on either object. You can actually call it like this if you want, though: `str.join(separator, iterable)`.
- ptx 5y agoThe way it's managed in JS, digging the function out of the prototype to apply it, can be done in Python as well. But unlike JS you won't normally have to, thanks to the method not being defined only on one specific type of iterable. JS: Array.prototype.join.call(["one", "two", "three"], "|") Python: str.join("|", ["one", "two", "three"])
- dragonwriter 5y ago> Yet Ruby and JS manage to do it somehow. Ruby does it by having a mixin (Enumerable) that anything meeting a basic contract (roughly equivalent to the Python iterable protocol) can include to get an enormous block of functionality; Python doesn’t have (or at least idiomatically use as freely; ISTR that there is a way to do it) mixins like Ruby does.
- deleted 5y ago[deleted]
- Someone 5y agoIt may be necessary in python, but in general, a language could allow you to define a join method on the iterable superclasss/trait/interface that iterates over the elements, converting each to a string, and inserting the separator between each of them. For example, scala has Iterable#mkstring (https://docs.scala-lang.org/overviews/collections-2.13/trait-iterable.html#inner-main https://docs.scala-lang.org/overviews/collections-2.13/trait...)
- robluxus 5y agoIt's more about flexibility than consistency. str.join() and bytes.join() can support all iterable type arguments. Better than trying to implement a join (or two) on all iterables.
- pdonis 5y agoIn the cases you give, the original list is not being mutated; a new object (a string, not a list) is being created. So it does make sense not to have it be a method call on the list.
- rndgermandude 5y ago' '.join() makes more sense to me, and it's more universal too if done right and you accept anything resembling a "sequence" (which python does) and individual objects of the sequence have a sensible str(). And as language maintainer, you only have to maintain one such implementation, not one per collection type. Javascript, on the other hand, kinda does it worst, at least of the languages I regularly use... .join() is a instance method on Arrays and TypesArrays. But they forgot to add any kind of join for Sets, for example. (["a", "b", "c"]).join("") "abc" # alright (new Set(["a", "b", "c"])).join("") Uncaught TypeError: (intermediate value).join is not a function ([...new Set(["a", "b", "c"])]).join("") "abc" # grmpf, have to materialize it into an array first. That illustrates the drawback: if you make it a method on the concrete sequence types you got, you better not forget some and make sure the different APIs are consistent, too. If Javascript had a String.join(sep, <anything implementing the iterator protocol>) this wouldn't have been an issue. python isn't alone either, by the way. C# has the static string.Join(...) that accepts "enumerables" (IEnumerable<T>), but no array.Join() or list.Join() or dictionary.Join(). Combined with Linq, especially .Select, that becomes quite handy. It has been plenty of times I did print-debugging by adding a one liner along the lines of Console.WriteLine(string.Join("\n", dictionary.Select((key, value) => $"{key} = {value.SomeProperty}"))); I find the C# way of having a string.Join(sep, ...) instead of python's "some string".join(...) nicer to read because it's more obvious.
- pansa2 5y ago> I find the C# way of having a string.Join(sep, ...) instead of python's "some string".join(...) nicer to read because it's more obvious. `str.join(sep, ...)` works in Python as well, because `a.f(...)` and `type(a).f(a, ...)` are (almost) equivalent.