Recursive list comprehension

Steven Bethard steven.bethard at gmail.com
Wed Dec 8 22:20:34 CET 2004


Adam DePrince wrote:
> On Wed, 2004-12-08 at 15:02, Steven Bethard wrote:
>>Note that I special-case strings because, while strings support the 
>>iterator protocol, in this case we want to consider them 'atomic'.  By 
>>catching the TypeError instead of an AttributeError, I can support 
>>old-style iterators as well.
> 
> Of course, iter( "a" ).next() is "a" -- if you don't look for the
> special case of a string you will spin until you blow your stack.   The
> problem with a special case is it misses objects that have string like
> behavior but are not members of basestring.

Yup.  Unfortunately, there's no "string protocol" like there's an 
"iterator protocol" or we could check this kind of thing easier.  Seems 
like your history check might be the best option if you need to support 
this kind of thing.

Steve



More information about the Python-list mailing list