[Tutor] flatten a tuple

Bruce Sass bsass@freenet.edmonton.ab.ca
Fri, 20 Apr 2001 12:22:47 -0600 (MDT)


On Fri, 20 Apr 2001, D-Man wrote:
<...>
> - if  type( L ) == type( [] ) :
> + import types
> + if  type( L ) == types.ListType :
>
> The difference is the first line creates a new list, calls a function
> to get its type, then frees the list each time it is executed.  The
> second one creates a list, gets its type, frees it only on the import.
> OTOH the second form does the same thing for every other type, so it
> really depends on where you want the time to be spent.  If you check
> the type of other stuff or if you run through the loop a lot it is a
> good tradeoff.

I'd say the best way is to do...

    ListType = type([])
    ...
    if type(L) == ListType

The first line is what "import types" does; doing it manually will
always be faster because there is no "import" or attribute access
(".") overhead.


- Bruce