list comprehension question

Lie Ryan lie.1296 at gmail.com
Thu May 7 05:23:30 EDT 2009


Scott David Daniels wrote:
> 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."

Maybe that is why some modules like PIL or pygame accepts list of 
coordinates in flattened list of number instead of nested (i.e. they 
accepts [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] and do the grouping 
themself).



More information about the Python-list mailing list