<div dir="ltr"><div dir="ltr"><div>Disclaimer: I've not recently read any discussions of this topic. And everything I say is my own opinion.</div><div><br></div><div>SUMMARY</div><div>The syntax of Python's string join operator are a consequence of Python's core design decisions and coding principles, together with the semantics of join. I'll explain this in a series of posts.</div><div><br></div><div>Why str.join? Wouldn't list.join be better?</div><div>===============================</div><div>Python variables don't have types. Python objects do have types. When writing Python, don't test for the type of an object, unless you have to. Instead, test to see if the object has the methods you need.</div><div><br></div><div>Eg help(dict.update)</div><div><div>update(...)</div><div> D.update([E, ]**F) -> None. Update D from dict/iterable E and F.</div><div> If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]</div><div> If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v</div><div> In either case, this is followed by: for k in F: D[k] = F[k]</div><div><br></div></div><div>Now for the semantics of string join. By Python principles</div><div> From a str and an iterable</div><div> The string join operator</div><div> Produces a str</div><div><br></div><div>You question amounts to this. Should the signature be</div><div> (str, iterable) -> str # Informal type signature of join.<br></div><div>or should it be</div><div> (iterable, str) -> str</div><div><br></div><div>At first glance, there's no way of choosing. But in Python we prefer</div><div> aaa.join(bbb)</div><div>to</div><div> from somewhere import join</div><div> join(aaa, bbb)</div><div><br></div><div>So the question now becomes: Choose between</div><div> str.join(iterable)</div><div> iterable.join(str)</div><div><br></div><div>There is some sense in having list.join. But in Python, we can't join the elements of a list without getting an iterable from the list (unless there's something like very special short-cut-semantics built into Python).</div><div><br></div><div>So in Python the choice is between</div><div> str.join(iterable)</div><div> iterable.join(str)</div><div><br></div><div>Now str.join looks more attractive. But I said, without thinking ahead, that the syntax of Python's string join operator is a consequence of Python's core design decisions and coding principles, together with the semantics of join.</div><div><br></div><div>I'm not quite there yet, there's a gap to fill. I'll pause for a day or two now, just in case someone else wants to JOIN in the discussion, and perhaps fill the gap.</div><div><br></div><div>-- </div><div>Jonathan</div><div><br></div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
</blockquote></div></div></div>