[Tutor] flatten a tuple

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 20 Apr 2001 11:45:07 -0700 (PDT)


On Fri, 20 Apr 2001, Bruce Sass wrote:

> 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.


Deja vu!  *grin*

    http://mail.python.org/pipermail/tutor/2001-March/003911.html