首頁  >  問答  >  主體

為什麼python中實例檢查推薦使用isinstance而不是type?

http://codingpy.com/article/p...

實例檢查:

使用isinstance(a, C),而不是type(a) is C`。但是一般要避免做實例檢查。建議檢查實例的特性。

阿神阿神2705 天前836

全部回覆(2)我來回復

  • PHP中文网

    PHP中文网2017-05-24 11:37:09

    當遇到比較物件是子類別實例與父類別時,isinstance会认为相等,type則不等。

    相當於 isinstancetype 多了个检查继承的功能,所以更愿意选择 isinstance

    回覆
    0
  • 世界只因有你

    世界只因有你2017-05-24 11:37:09

    雷雷

    回覆
    0
  • 取消回覆