[Python-3000] iter_flatten was: Making strings non-iterable

Raymond Hettinger RHettinger at ewtllc.com
Tue Apr 18 01:47:18 CEST 2006


[Brian Harring]
> The issue I'm seeing is that the wart you're pointing at is a
>  general issue not limited to strings- everyone sooner or later
>  has flattening code that hits the "recursively iterate over
>  this container, except  for instances of these classes".  

Good recap.  For more info, search comp.lang.python for previous
discussions about a proposed iter_flatten() function.  One of the
conclusions was that a general algorithm would need to accept a user
specified test for atomicity so it would know where to recurse and a
where not to (the answer to that is application dependent).

Another conclusion was that an iterator version of flattening was a just
one of many possible algorithms for traversing a hierarchical structure
(i.e. a pre-order tree traversal).  But some use cases needed more
control points than provided by overly specific solutions (os.walk() and
depth-first puzzle solvers are prime examples).

The discussion itself was an interesting case-study because the
flattening algorithm variants out-numbered the use cases (IOW, it's more
fun to write flatten() than to use it).  It was also interesting because
the problem produced a tail-wagging-the-dog effect as discussions went
from "how can I write a more general flatten" and turned into "how can I
can modify the language to make my flatten variant apply more
generally".


Raymond


More information about the Python-3000 mailing list