The .join() that should be a bug
The article explores a surprising behavior in Python's string `.join()` method: calling `"".join([1, 2, 3])` raises a `TypeError`, not silently converting integers. This exposes a design inconsistency where `.join()` demands iterable elements be strings, unlike other APIs that perform implicit type coercion, making it a common point of confusion for developers.
Background
- Python's `str.join()` method requires a list of strings as its argument. If you accidentally pass a single string, Python iterates over its characters, joining them with the separator — producing a confusing result instead of an error.
- Example: `", ".join("hello")` returns `"h, e, l, l, o"` rather than raising a `TypeError`. This happens because a string is iterable, and `.join()` works on any iterable, not just lists.
- This design choice is a consequence of Python's duck-typing philosophy ("if it walks like a duck, it quacks like a duck"), but it silently hides a common beginner mistake — passing a single string where a list of strings was intended.
- The post argues that this behavior should be a bug (or at least trigger a warning), because it violates the principle of least surprise: `join` semantically combines *multiple* things, so accepting a single thing and silently splitting it is misleading.