JT.
JT.

Reputation: 351

Is 'dict', False. Yet gives <type 'dict'>?

>>> flob = {u'': [u''], u'DL': [u'DL'], u'Gender': [u'Both Sexes'], u'SQ': [u'SQ'], u'BP': [u'BP'], u'Location': [u'World Wide']}

>>> flob is dict

False

>>> flob == dict

False

>>> type(flob)

<type 'dict'>

I ask it if its type dict, it says false. I ask it what type it is, it says dict. Why?

Upvotes: 1

Views: 1680

Answers (3)

Karl Knechtel
Karl Knechtel

Reputation: 61597

Is this orange the same thing as the concept of oranges in general?

Is this orange equal in value to the concept of oranges in general?

What kind of thing is this orange?

Upvotes: 3

wim
wim

Reputation: 362944

is is used or identity comparison. See below for a hint - id returns the memory location of an object and id(a) == id(b) is analogous to a is b. For type checks (which should be used sparingly), you want isinstance.

>>> flob = {u'': [u''], u'DL': [u'DL'], u'Gender': [u'Both Sexes'], u'SQ': [u'SQ'], u'BP': [u'BP'], u'Location': [u'World Wide']}
>>> id(flob)
17665920
>>> id(dict)
8678496
>>> id(flob) == id(dict)
False
>>> type(flob) == dict
True
>>> isinstance(flob, dict)
True

Upvotes: 3

Donald Miner
Donald Miner

Reputation: 39913

Because you are comparing the object itself, the instance of dict, with dict... these two things are different entities. is is used to determine if two variable names are pointing at the same object.

 type({}) is dict  # returns true.

 a = {}
 b = a
 a is b  # returns true

Upvotes: 8

Related Questions