What is the command to determine an object's type?

Bengt Richter bokr at oz.net
Wed Feb 6 18:37:19 EST 2002


On Wed, 06 Feb 2002 20:31:44 +0100, Tino Lange <tl_news at nexgo.de> wrote:

>On Tue, 05 Feb 2002 22:38:49 GMT, "Fredrik Lundh"
><fredrik at pythonware.com> wrote:
>
>Hi Fredrik!
>
>(1) if type(l) == types.ListType:
>>or better:
>(2) if type(l) is types.ListType: 
>>or even better:
>(3) if isinstance(l, types.ListType):
>
>Why is "if isinstance(l, types.ListType):" even better than the rest?
>What do you mean by "better"?
>
>I just compared the speed of execution (using Python 2.1 on Linux) and
>found consistently:
>(2) is the fastest, the comes (3), (1) is much slower.
>
I suspect you might not have been timing what you thought you
were timing, but I could be wrong ;-)

>So I would guess (2) is the most effective implementation?
>
Per quick check, apparently not so on Windows Python 2.1. 
(3) is fastest, (2) is next, and (1) slowest.

[15:30] C:\pywk\crdtsc>python endsvslice9.py 10 10 850 1000 5
10 loops, 1000 avgloops, 850 tadj 5 ntrials
  0: eqeq_types:  4429, avg  4673
  1: eqeq_types:  4457, avg  4679
  2: eqeq_types:  4487, avg  4680
  3: eqeq_types:  4527, avg  4685
  4: eqeq_types:  4501, avg  4680

[15:33] C:\pywk\crdtsc>python endsvslice9.py 11 10 850 1000 5
10 loops, 1000 avgloops, 850 tadj 5 ntrials
  0:   is_types:  4105, avg  4333
  1:   is_types:  4146, avg  4322
  2:   is_types:  4204, avg  4319
  3:   is_types:  4152, avg  4328
  4:   is_types:  4096, avg  4318

[15:34] C:\pywk\crdtsc>python endsvslice9.py 12 10 850 1000 5
10 loops, 1000 avgloops, 850 tadj 5 ntrials
  0: is_instance:  4168, avg  4191
  1: is_instance:  4037, avg  4147
  2: is_instance:  4032, avg  4154
  3: is_instance:  4068, avg  4139
  4: is_instance:  4003, avg  4140




More information about the Python-list mailing list