Itertools wishlists

Steven Bethard steven.bethard at gmail.com
Mon Mar 14 13:12:47 EST 2005


Robert Brewer wrote:
> Steven Bethard wrote:
> 
>>Ville Vainio wrote:
>>
>>>A simpler API:
>>>
>>>def flatten(sequence, atomic_test = lambda o: 
>>
>>isinstance(o,basestring)):
>>
>>>  """ don't recurse into iterables if atomic_test -> True """
>>
>>Yes, this is also the API I would have suggested.  Simple, 
>>but flexible enough to handle the odd cases with the occasional 
>>user-defined iterable non-containers.
> 
> If there are two or three common atomic_test's they could also be placed
> into itertools globals (with better names, of course ;):
> 
> def is_string(item):
>     return isinstance(item, basestring)
> 
> def flatten(seq, atomic_test = is_string):
>     ...
> 
> Perhaps atomic_test could allow a tuple or list of tests and combine
> them...?

Did you have other atomic tests in mind?  All the use cases I've ever 
seen have always been with strings.  (And I've never created any class 
whose items are instances of the class.)

I don't see much benefit in complicating the API of flatten unless 
there's a really good use case for multiple atomic tests (that can't 
just as easily be solved by writing your own function that does the 
appropriate complex atomicity test).  I also have the feeling that any 
complicated atomictiy test is more than a simple and-ing of several tests...

STeVe



More information about the Python-list mailing list