我无法弄清楚我的代码有什么问题:
for key in tmpDict:
print type(tmpDict[key])
time.sleep(1)
if(type(tmpDict[key])==list):
print 'this is never visible'
break
输出是<type 'list'>
但 if 语句永远不会触发。有人能在这里发现我的错误吗?
你应该尝试使用isinstance()
if isinstance(object, list):
## DO what you want
在你的情况下
if isinstance(tmpDict[key], list):
## DO SOMETHING
详细说明:
x = [1,2,3]
if type(x) == list():
print "This wont work"
if type(x) == list: ## one of the way to see if it's list
print "this will work"
if type(x) == type(list()):
print "lets see if this works"
if isinstance(x, list): ## most preferred way to check if it's list
print "This should work just fine"
和...之间的不同isinstance()
and type()
虽然两者似乎都做同样的工作isinstance()
另外检查子类,同时type()
没有。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)