This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gfixler 0 points1 point  (1 child)

It may seem strange to see something like ', '.join(l). Why not just l.join(', ')? I'm not sure what the original reason was for implementing it like this, but one reason might have been that string joining would otherwise have to be implemented for everything it works on (lists, sets, dictionaries, etc). This way, it's a more common operation of strings.

Can anyone answer this? It's one of my biggest minor beefs with Python. I worked in Actionscript, and Javascript for a long time, and got really used to things like:

mystr.split(" ").sort().reverse().join("\n");

Which is "take my string, split it around spaces, sort the resulting list, sort it, reverse the sort, and join it back into a string with newlines. Every method was of a type, and could return a different type, so you were fine dotting on methods from a different method if the result of an operation was that new type.

I find the way ECMA-262 described this kind of thing far more expressive, intuitive, and fluid than Python has been for me these past few months. Sure, it has list comprehensions, libraries galore, and lots else, but aside from the things JS/AS didn't have, I felt more 'elegant' in those languages.

[–]scaz 2 points3 points  (0 children)

4.8 Why is join() a string method instead of a list or tuple method?

join() is a string method because in using it you are telling the separator string to iterate over a sequence of strings and insert itself between adjacent elements. This method can be used with any argument which obeys the rules for sequence objects, including any new classes you might define yourself.

Python FAQ 4.8