Checking if a variable is a dictionary

Andrew Koenig ark at acm.org
Mon Mar 10 10:29:48 EDT 2008


>> if type(a) is dict:
>>     print "a is a dictionnary!"

> class MyDict(dict):
>     pass

> a = MyDict()

> type(a) is dict
> => False

isinstance(a, dict)
=> True

So the question you need to answer is whether you want to determine whether 
an object is exactly of type dict, or whether it you are willing to accept 
types derived from dict also.





More information about the Python-list mailing list