Chanelling Guido - dict subclasses

Devin Jeanpierre jeanpierreda at gmail.com
Thu Jan 16 01:30:53 EST 2014


On Wed, Jan 15, 2014 at 8:51 AM, John Ladasky
<john_ladasky at sbcglobal.net> wrote:
> On Wednesday, January 15, 2014 12:40:33 AM UTC-8, Peter Otten wrote:
>> Personally I feel dirty whenever I write Python code that defeats duck-
>> typing -- so I would not /recommend/ any isinstance() check.
>
> While I am inclined to agree, I have yet to see a solution to the problem of flattening nested lists/tuples which avoids isinstance().  If anyone has written one, I would like to see it, and consider its merits.

As long as you're the one that created the nested list structure, you
can choose to create a different structure instead, one which doesn't
require typechecking values inside your structure.

For example, os.walk has a similar kind of problem; it uses separate
lists for the subdirectories and the rest of the files, rather than
requiring you to check each child to see if it is a directory. It can
do it this way because it doesn't need to preserve the interleaved
order of directories and files, but there's other solutions for you if
you do want to preserve that order. (Although they won't be as clean
as they would be in a language with ADTs)

-- Devin



More information about the Python-list mailing list