A different way to check the type of a list

后端 未结 2 1741
长发绾君心
长发绾君心 2021-01-15 03:51
list = [1,2,3]
print(type(list) == list) # Prints False

Other than changing the name of the list, is there another way to check the type of this li

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-15 04:37

    Your code justly returns False, as you replaced the original meaning of list by your list. You shouldn't use the names of Python builtins as variable names.

    So, change the name of your list and it will work as expected.

    If it's too late for that, as you suggest in the edit to your question, you can still access the original list with:

    list = [1,2,3]
    print(type(list) == __builtins__.list) 
    # True
    

    Or, the more recommended way, using isinstance instead of type(...) == ...:

    print(isinstance(list, __builtins__.list))
    # True
    

提交回复
热议问题