itertools.flatten()? and copying generators/iterators.

Peter Otten __peter__ at web.de
Thu Oct 30 06:14:54 EST 2003


Francis Avila wrote:

> eliminate the final argument to flatten_itercond.  Although, what would be
> the key for cache dict, such that it never gets overwritten?

Using your identifier names, instead of try ... except KeyError:

descend = do_descend.get(iterable.__class__, defaultFunction)

It's not there, so it won't get overwritten. You can think of the except
clause as a hard-coded defaultFunction(), so this approach would spare you
the exception that I expected to be rare in my original flatten_dict()
design.
I think your flatten() is getting more complex fast, and you're reaching the
point where you should either use a version designed for the special
purpose or pay the performance price and go with a simple test function
like my keepstrings() somewhere above.

> I really need to stop thinking about this silly flatten() function, but
> here's a quick hack which passes the hastily-modified doctests:

Me too :-)

Peter

PS: The Martelli virus spreading fast: have you timed it recently?




More information about the Python-list mailing list