How do I check (at runtime) if one class is a subclass of another?

前端 未结 9 1775
忘了有多久
忘了有多久 2020-11-28 05:40

Let\'s say that I have a class Suit and four subclasses of suit: Heart, Spade, Diamond, Club.

class Suit:
   ...
class Heart(Suit):
   ...
class Spade(Suit):         


        
9条回答
  •  死守一世寂寞
    2020-11-28 05:53

    You can use isinstance if you have an instance, or issubclass if you have a class. Normally thought its a bad idea. Normally in Python you work out if an object is capable of something by attempting to do that thing to it.

提交回复
热议问题