Preferred way of determining type
Alex Martelli
aleaxit at yahoo.com
Wed May 16 17:31:47 EDT 2001
"Emile van Sebille" <emile at fenx.com> wrote in message
news:9dueq7$4slq$1 at ID-11957.news.dfncis.de...
> 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?
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)!-)
http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52291
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!-)
Alex
More information about the Python-list
mailing list