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"
您的问题是您已经在代码中重新将
list
定义为变量。这意味着当您执行type(tmpDict[key])==list
操作时,if将返回False
,因为它们不相等。也就是说,您应该在测试某个类型时使用^{} ,这并不能避免覆盖
list
的问题,而是检查该类型的一种更为python的方法。这似乎对我有用:
您应该尝试使用
isinstance()
对你来说
详细说明:
相关问题 更多 >
编程相关推荐