What are the differences between these two code fragments?
Using type()
:
import types
if type(a) is types.DictType:
do_something(
A practical usage difference is how they handle booleans
:
True
and False
are just keywords that mean 1
and 0
in python. Thus,
isinstance(True, int)
and
isinstance(False, int)
both return True
. Both booleans are an instance of an integer. type()
, however, is more clever:
type(True) == int
returns False
.