Flattening lists

Michele Simionato michele.simionato at gmail.com
Fri Feb 6 23:40:20 EST 2009


On Feb 6, 10:23 pm, Rhamphoryncus <rha... at gmail.com> wrote:
> On Feb 5, 1:16 pm, Michele Simionato <michele.simion... at gmail.com>
> wrote:
>
> > On Feb 5, 7:24 pm, a... at pythoncraft.com (Aahz) wrote:
>
> > > In article <a22c77c4-a812-4e42-8972-6f3eedf72... at l33g2000pri.googlegroups.com>,
> > > Michele Simionato  <michele.simion... at gmail.com> wrote:
>
> > > >Looks fine to me. In some situations you may also use hasattr(el,
> > > >'__iter__') instead of isinstance(el, list) (it depends if you want to
> > > >flatten generic iterables or only lists).
>
> > > Of course, once you do that, you need to special-case strings...
>
> > Strings are iterable but have no __iter__ method, which is fine in
> > this context, since I would say 99.9% of times one wants to treat them
> > as atomic objects, so no need to special case.
>
> Don't worry, that little oddity was fixed for you:

Acc! I have a few places in my code with checks of the
kind ``hasattr(x, '__iter__')`` and I guess those spots
will be tricky when converting to Python 3. I guess
2to3 cannot help either :-(



More information about the Python-list mailing list