Python issubclass()
Preview: Anonymous contributor 1 total contribution
Anonymous contributor
Published Mar 27, 2023
The issubclass() function returns a boolean indicating whether a given class is a subclass of one or more classes.
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 CoursesIncludes 6 Courses
- With Professional CertificationWith Professional Certification
- Beginner Friendly.75 hours75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With CertificateWith Certificate
- Beginner Friendly.24 hours24 hours
Syntax
issubclass(class, candidateClasses)
issubclass() take two parameters:
class: A validly defined class.candidateClasses: A single class or type, or a tuple of classes and types.
If class is a subclass of candidateClasses (direct or indirect), True is returned, False otherwise.
Example
The following example uses the issubclass() function to confirm whether myInfo is a subclass of the myAge class:
classmyAge:age =36classmyInfo(myAge):name ="Homer"age = myAgeprint(issubclass(myInfo, myAge))Copy to clipboardCopy to clipboard
This will print the following output:
TrueCopy to clipboardCopy to clipboard
Codebyte Example
In the following example, the issubclass() function checks the relationship between the Car and Color classes. While Color is a subclass of Car, the reverse is not true:
Visit usVisit usCodeHide codeOutputHide outputCopy to your clipboard
All contributors
- Preview: Anonymous contributor 1 total contribution
- Anonymous contributor
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 CoursesIncludes 6 Courses
- With Professional CertificationWith Professional Certification
- Beginner Friendly.75 hours75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With CertificateWith Certificate
- Beginner Friendly.24 hours24 hours
