What is the purpose of checking self.__class__ ? - python

后端 未结 5 952
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 14:04

What is the purpose of checking self.__class__ ? I\'ve found some code that creates an abstract interface class and then checks whether its self.__class__

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 14:52

    The code that you posted there is a no-op; self.__class__ == c1 is not part of a conditional so the boolean is evaluated but nothing is done with the result.

    You could try to make an abstract base class that checks to see if self.__class__ is equal to the abstract class as opposed to a hypothetical child (via an if statement), in order to prevent the instantiation of the abstract base class itself due to developer mistake.

提交回复
热议问题