On Tue, Apr 13, 2021 at 05:39:42PM -0000, Dennis Sweeney wrote:
Whenever you extend the definition of an operation (`__iter__` in this case) to more existing objects, you lose a little bit of the ability to catch errors early. Consider the function:
def traverse(something): for x in something: # do stuff ...
If you accidentally call `traverse(42)`, then right now, you catch it immediately with a TypeError on the `for x in something:` line. Under your proposal, you might just get a strange answer and not realize anything is wrong.
Error? Perhaps feature! You will get the desired behaviour and don't need to handle corner case like this. from collections.abc import Iterable def traverse(s): for x in s if isinstance(s, Iterable) else (s,): print(f"{x=}") traverse(5) traverse(range(3)) H.