[Python-Dev] Comparison speed
Jeremy Hylton
jeremy@digicool.com
Tue, 15 May 2001 13:26:54 -0400 (EDT)
I only learned recently that isinstance() can be called with types
instead of classes. I suppose the name lead me in the wrong
direction. I had the silly idea that it only applied to instances
<0.1 wink>.
So it comes as little surprise to me that there is a lot of code
executed in, e.g., the test suite that does comparisons on types.
In the Lib directory, there are 63 files that use == and the builtin
type function. (Simple grep.) A total of 139 instances of this
idiom. A cursory scan suggests that most of the call are things like
type(obj) == type('').
In the Zope source tree, there are 58 files and 98 individual
occurrences. It again looks like comparisons against string type is
the most common.
I can think of two common cases where an object is checked against the
string type. One is an interface that takes a file-like object or its
path. The other is an interface that takes a sequence, but doesn't
want to try a string as a sequence.
Sounds like we ought to do a search-and-destroy on type comparisons,
replacing with isinstance() where possible.
Jeremy