list comprehension question

Scott David Daniels Scott.Daniels at Acm.Org
Fri May 1 15:03:39 EDT 2009


John Posner wrote:
> Shane Geiger wrote:
>>    if type(el) == list or type(el) is tuple:
> A tiny improvement:
> 
>        if type(el) in (list, tuple):

or (even better)
     if isinstance(el, (list, tuple))

However, it is my contention that you shouldn't be flattening by type --
you should know where, explicitly to flatten.  If I have 3-D points as
triples, a tree of points would, by this code, get flattened into a
a list of numbers.   If, however, I create a class with the same
elements, but a method or two, suddenly flatten will produce a list
of points.  It is my contention that the tree abstraction should be
responsible for producing its leaves, rather than a blunderbus that
doesn't know where one container abstraction ends, and its contents
begin.  In other words, I'd like to see thigs like "flatten one layer."

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list