Checking if type == list in python

后端 未结 5 1705
孤城傲影
孤城傲影 2020-12-01 04:08

I may be having a brain fart here, but I really can\'t figure out what\'s wrong with my code:

for key in tmpDict:
    print type(tmpDict[key])
    time.sleep         


        
5条回答
  •  时光取名叫无心
    2020-12-01 04:41

    Your issue is that you have re-defined list as a variable previously in your code. This means that when you do type(tmpDict[key])==list if will return False because they aren't equal.

    That being said, you should instead use isinstance(tmpDict[key], list) when testing the type of something, this won't avoid the problem of overwriting list but is a more Pythonic way of checking the type.

提交回复
热议问题