Issubclass Function in Python
In Python, the issubclass
function is used to determine if one class is a subclass of another.
Syntax
issubclass(subclass, base_class)
The function takes two arguments: the subclass (the derived class) and the base_class (the parent class).
It returns a boolean value:
- True if the first class is a subclass of the second.
- False if it is not.
A Practical Example
Here’s a simple script with two classes:
The class Regioni
is defined as a subclass of the class Nazioni
.
- class Nazioni:
- nazione = "Italia"
- class Regioni(Nazioni):
- regione = "Lazio"
To check whether Regioni
is a subclass of Nazioni
, we can use the issubclass() function.
The function outputs True:
>>> issubclass(Regioni, Nazioni)
True
This confirms that Regioni
is indeed a subclass of Nazioni
.
And that’s it!