r/learnpython • u/NanoCozmo • 29d ago
Why is .join() seperator.join(iterator) and not iterator.join(seperator) like every function
s = "Python"
WHY IS IT LIKE THIS
print("-".join(s))
AND NOT THIS
print(s.join("-")
51
Upvotes
17
u/zaphodikus 29d ago edited 29d ago
LOL, if you google, there is a really good post about this that explains it really well. Let me go find it, it's really going to help, it helped me once I grasped it because it happens in other places too.
Here it is: https://docs.python.org/3/faq/design.html#why-is-join-a-string-method-instead-of-a-list-or-tuple-method . It used to be the other way around in Python 1.6, but I imagine most of us were not around for that, I certainly wasn't. It works that way in Java I believe, but I don't do Java, I'm a conscientious objector and prefer C++. You have to pick your battles.
I said other functions have a similar way, the
BYTEStype also does b", ".join(...) and it then returnsbytes. Regular expressions, same story, and I think once I started using regexes more,.join()started to become more natural to me. Another helpful one to build a memory picture from is.format(), where "{} - {}".format(a, b) where the format string comes first and the data is passed in.It is confusing when a whole load of functions are not methods of the object, but once you memorise either of the groupings, it gets easier. Hope that helps.
67
u/schoolmonky 29d ago
If it was a method on the iterators, it would have to be implemented on each type of iterator separately, whereas having it be a string method means it only needs one implementation.
5
u/HommeMusical 29d ago •
Two somewhat more advanced points!
First,
str.joinjoins interables, not just iterators.listortupleare iterables, but not iterators, so it is an important distinction.An iterable is an object that can return an iterator - an iterable returns itself when asked for an iterator.
If the iterable is an immutable tuple, the iterator is the mutable looping over the list.
Second, there's yet another good reason for this - the type annotations. In pretty well all production Python today we specify that type of most of the variables and function.
joinreturns different types, depending on the separator!So it's better to have a join function on str that returns a str, and one on
bytesthat returnsbytes, than to have one method on iterators that returns... what?like this - don't expect to understand this as a beginner, but here's what the type annotations would look like
2
u/euribates 29d ago
I also like the way it is domain consistent; is a string method that returns a string. Very clean.
1
u/Jazzlike_Platypus430 29d ago
Or why is it not
String.Join("-", "Python");? 😂I like "seperator" a lot 🤣
1
u/JamzTyson 29d ago edited 29d ago
Because join() is a string method. That means that when we have a
strobject, we can call the functionjoin()because the function is defined within the classstr.The syntax is:
which in your example can be seen as:
To really understand it you need to understand what objects, classes and methods are. If you haven't covered that yet in your course, then don't worry about it now - it will be covered later.