Preferred way of determining type

Alex Martelli aleaxit at
Wed May 16 17:31:47 EDT 2001

"Emile van Sebille" <emile at> wrote in message
news:9dueq7$4slq$1 at
> 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:
> type(obj) == type([]).
> Anyone care to explain why or in what circumstances there's a difference?

There will be an advantage one day to isinstance if it ever becomes
possible to inherit from types.  It's less likely than a == test on a
type object will work.  But type-testing is rarely optimal anyway --
why do you want to forbid obj being passed as a UserList and to
insist on a bona fide true list?  It's annoying enough when Python
itself does it (mostly for dictionaries, sometimes for other types)!-)
suggests a general approach to do without type testing when you
think you just can't do without it.  Mostly, you don't even need
that much -- try/except most often suffices!-)


More information about the Python-list mailing list