python type()和instance()區別

isinstance() function is used to judge one object is some type, this is familiar with function type()

the differences between isinstance() and type() is that isinstance() consider the inheritation.

class A:
    pass
 
class B(A):
	pass
 
isinstance(A(), A)    # returns True
type(A()) == A        # returns True
isinstance(B(), A)    # returns True
type(B()) == A        # returns False

isinstance() 與 type() 區別:

type() 不會認爲子類是一種父類類型,不考慮繼承關係。
isinstance() 會認爲子類是一種父類類型,考慮繼承關係。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章