Steven D'Aprano" email@example.com If built-in objects grew an __atomic__ attribute, you could simplify the atomic() function greatly:
I may not have been clear enough in my previous post. Atomicity is not an intrinsic property of an object or class. How could you know in advance what various applications would want to consider atomic? The decision is application specific and best left to the caller of the flattener:
def flatten(obj, predicate=None): if predicate is not None and predicate(obj): yield obj else: for item in obj: for i in flatten(item): yield i
However atomic() is defined, now flatten() is easy:
Rule of thumb: if you find a need to change the language (adding a new builtin, adding a new protocol, and adding a property to every builtin and pure python container) just to implement a simple recipe, then it is the recipe that needs fixing, not the language.
P.S. You're on the right track by factoring the decision away from the internals of flatten(); however, the atomic() predicate needs to be user definable for a given application not hardwired into the language itself.