python - How do I check (at runtime) if one class is a subclass of another? -
let's have class suit , 4 subclasses of suit: heart, spade, diamond, club.
class suit: ... class heart(suit): ... class spade(suit): ... class diamond(suit): ... class club(suit): ... i have method receives suit parameter, class object, not instance. more precisely, may receive 1 of 4 values: heart, spade, diamond, club. how can make assertion ensures such thing? like:
def my_method(suit): assert(suit subclass of suit) ... i'm using python 3.
you can use issubclass() assert issubclass(suit, suit).
but why want such thing? python not java.
Comments
Post a Comment