which is better: isinstance() or type()?

Greg Ewing see_reply_address at something.invalid
Fri Aug 2 03:37:49 CEST 2002

Donnal Walter wrote:

> Benjamin Han <bhan at andrew.cmu.edu> wrote in message news:<Pine.LNX.4.44.0208011324350.3775-100000 at capriccio.harmony.org>...

>>print isinstance(f,Foo)
>>print type(f) is Foo
>>so which one is better?

> Not long ago I asked much the same question. Here is Neal's answer:
>>>I read in Guido's keynote address (slide 15 of 23) that we are now
>>>supposed to be using built-in type names instead of the types module.

But that's answering a different question.

My answer to the original question is: Always use isinstance(),
unless it matters that you have *exactly* a particular type
and not a subclass of it. Cases of that should be pretty
rare, perhaps even nonexistent.

Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand

More information about the Python-list mailing list