What are the differences between type() and isinstance()?

后端 未结 7 2351
青春惊慌失措
青春惊慌失措 2020-11-21 06:06

What are the differences between these two code fragments?

Using type():

import types

if type(a) is types.DictType:
    do_something(         


        
7条回答
  •  悲&欢浪女
    2020-11-21 06:50

    According to python documentation here is a statement:

    8.15. types — Names for built-in types

    Starting in Python 2.2, built-in factory functions such as int() and str() are also names for the corresponding types.

    So isinstance() should be preferred over type().

提交回复
热议问题