Preferred way of determining type

Tom Jenkins tjenkins at devis.com
Wed May 16 16:27:10 EDT 2001


Emile van Sebille wrote:

> More frequently lately it seems that isinstance(obj, types.Type) or simply
> if type(obj) == types.Type is recommended over what I regularly use, eg:  if
> type(obj) == type([]).
> 
> Anyone care to explain why or in what circumstances there's a difference?
> 

At the recent Python conference in Long Beach I overheard Guido say that 
isinstance should be the way you check for type equality.

Example:
if isinstance(obj, type([])):
    #do whatever here

The reason is that isinstance will continue to work once the type/class 
split is mended, but he did not think that type(obj) == type([]) would 
not.  I distinctly remember this because I made a note to myself to tell 
my folks here to stop doing it the way you (and I) regularly used.  But 
again this was overheard, I have not code to explain why.

-- 
Tom Jenkins
devIS - Development Infostructure
http://www.devis.com





More information about the Python-list mailing list