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):
...
class Diamond(Suit):
...
class Club(Suit):
...
I have a method which receives a suit as a parameter, which is a class object, not an instance. More precisely, it may receive only one of the four values: Heart, Spade, Diamond, Club. How can I make an assertion which ensures such a thing? Something like:
def my_method(suit):
assert(suit subclass of Suit)
...
I'm using Python 3.
my_method
can get as parameters: "it may receive only one of the four values: Heart, Spade, Diamond, Club". Those values are class objects, not class instances. It seems pretty clear to me, though I suppose you're right about the vagueness because the answers do cover both possibilities. You're more than welcome to edit the question if you've got a clearer wordage for it. Thanks for the comment. – Nuncia_class
, making them likesuit_class
. I proposed such a naming convention in a relevant question. – Contemptuousmy_method(Heart)
my_method(Spade)
... – Catheryncatheterinspect.isclass
or simply useisinstance(myvar, type)
in Python 3, asissubclass
will raise an error if it's passed a non-class. See this answer. I would have commented on the answer below, but it never would have seen the light of day. – Coadunate