json - checking whether variable is dictionary in python - use 'is' or == -
summary: have variable called 'parent'
dictionary in python. want check if dict
object. however, using "type(parent) dict"
gives me 'false'
.
note: have next library loaded in python script:
from google.appengine.ext import ndb
why happening? suspected @ first because variable 'parent'
created using json
library's 'loads'
method.
parent = json.loads(self.request.body)
however, when create parent so,
parent = {}
i the same results observed below:
print type(parent) >> <type 'dict'> print type(parent) dict >> false print type({}) type(parent) >> true print type(parent) == dict >> false print type({}) == type(parent) >> true
what's going on here? python version issue? or have fact i've loaded google's app engine library? when execute next commands in normal terminal, no libraries loaded (python 2.7.5), next results, expect:
python 2.7.5 (default, sep 12 2013, 21:33:34) [gcc 4.2.1 compatible apple llvm 5.0 (clang-500.0.68)] on darwin >>> parent = {} >>> print type(parent) <type 'dict'> >>> print type(parent) dict true >>> print type({}) dict true >>> print type({}) type(parent) true >>> print type({}) == type(parent) true
thanks in advance guidance!
what's happening gae using subclass of dict
behind scenes.
the idiomatic way check whether object instance of type in python isinstance()
built-in function:
>>> parent = {} >>> isinstance(parent, dict) true
... works instances of type itself, , of subclasses.
python json google-app-engine
No comments:
Post a Comment