Recursive list comprehension
Nick Craig-Wood
nick at craig-wood.com
Wed Dec 8 15:30:01 EST 2004
Adam DePrince <adam at cognitcorp.com> wrote:
> def flatten( i ):
> try:
> i = i.__iter__()
> while 1:
> j = flatten( i.next() )
> try:
> while 1:
> yield j.next()
> except StopIteration:
> pass
> except AttributeError:
> yield i
Hmm, there is more to that than meets the eye! I was expecting
print list(flatten("hello"))
to print
['h', 'e', 'l', 'l', 'o']
But it didn't, it printed
['hello']
With a little more investigation I see that str has no __iter__
method. However you can call iter() on a str
>>> for c in iter("hello"): print c
...
h
e
l
l
o
Or even
>>> for c in "hello": print c
...
h
e
l
l
o
...and this works because str supports __getitem__ according to the
docs.
So there is some magic going on here! Is str defined to never have an
__iter__ method? I see no reason why that it couldn't one day have an
__iter__ method though.
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list