search

Home  >  Q&A  >  body text

Why is it recommended to use isinstance instead of type for instance checking in python?

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

Example check:

Use isinstance(a, C) instead of type(a) is C`. But generally avoid doing instance checking. It is recommended to check the characteristics of the instance.

阿神阿神2751 days ago871

reply all(2)I'll reply

  • PHP中文网

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

    When the comparison object is a subclass instance and a parent class, isinstance会认为相等,typeis not equal.

    is equivalent to isinstancetype 多了个检查继承的功能,所以更愿意选择 isinstance.

    reply
    0
  • 世界只因有你

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

    /q/1010000000127305
    看这个,更详细一点

    reply
    0
  • Cancelreply