The issubclass()
method is used to determine if a class is a subclass of a specified class.
True
if a class is a subclass of the specified classFalse
if a class is not a subclass of the specified classclassinfo can be specified as a tuple of class objects. In that case, every entry in classinfo is checked.
isubclass()
returnsTrue
if class is a subclass of any element in the tuple.
In the code below, we check if the Kiwi
class is a subclass of the Fruit
class. Since it is, issubclass()
returns True
.
class Fruit:def __init__(fruittype):print(fruittype)class Kiwi(Fruit):def __init__(self):Fruit.__init__('Kiwi')print(issubclass(Kiwi, Fruit))
In the following code snippet, we check if Kiwi
is a subclass of Fruit
and Vegetable
. Since Kiwi
is a subclass of Fruit
, issubclass()
returns True
.
class Fruit:def __init__(fruittype):print(fruittype)class Vegetable:def __init__(vegetabletype):print(fruittype)class Kiwi(Fruit):def __init__(self):Fruit.__init__('Kiwi')print(issubclass(Kiwi, (Fruit, Vegetable)))
Free Resources