好消息好消息,本 xx 已经和 Google 达成战略合作,大家有问题都可以去查 Google 啦。
(哪怕去查下 Google 也不会有这样的疑问,把 stack overflow 的回答贴过来:
https://stackoverflow.com/questions/493819/why-is-it-string-joinlist-instead-of-list-joinstringGuido 当时讨论的 thread,往后翻几页就能看到了:
https://mail.python.org/pipermail/python-dev/1999-June/095366.htmlThere were four options proposed in this thread:
str.join(seq)
seq.join(str)
seq.reduce(str)
join as a built-in function
1. Guido wanted to support not only lists, tuples, but all sequences/iterables.
2. seq.reduce(str) is difficult for new-comers.
3. seq.join(str) introduces unexpected dependency from sequences to str/unicode.
4. join() as a built-in function would support only specific data types. So using a built in namespace is not good. If join() supports many datatypes, creating optimized implementation would be difficult, if implemented using the __add__ method then it's O(n²).
5. The separator string (sep) should not be omitted. Explicit is better than implicit.