Python issubclass() Function
issubclass()It is a built-in function in Python used to check whether a class is a subclass of another class.
issubclass()It is used to determine the inheritance relationship between classes, and can check direct and indirect subclasses.
Word Definitions: issubclass 是 is a subclass(是一个子类)的缩写。
Basic Syntax and Parameters
Syntax
issubclass(class, classinfo)
Parameter Description
- Parameter class:
- Type: class
- Description: The class to be checked.
- Parameter classinfo:
- Type: class or tuple of classes
- Description: The parent class or a tuple of classes to check against.
Function Description
- Return Value: Returns a boolean value
TrueorFalse。 - Feature: A class is always considered a subclass of itself.
Examples
Example 1: Basic Usage
Example
# Class definition
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
class Husky(Dog):
pass
# Check inheritance relationship
print(issubclass(Dog, Animal)) # Output: True
print(issubclass(Cat, Animal)) # Output: True
print(issubclass(Dog, Cat)) # Output: False
# Indirect inheritance
print(issubclass(Husky, Animal)) # Output: True (inheritance chain: Husky -> Dog -> Animal)
print(issubclass(Husky, Dog)) # Output: True
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
class Husky(Dog):
pass
# Check inheritance relationship
print(issubclass(Dog, Animal)) # Output: True
print(issubclass(Cat, Animal)) # Output: True
print(issubclass(Dog, Cat)) # Output: False
# Indirect inheritance
print(issubclass(Husky, Animal)) # Output: True (inheritance chain: Husky -> Dog -> Animal)
print(issubclass(Husky, Dog)) # Output: True
Expected output:
True True False True True
Code analysis:
- The subclass is a subclass of the parent class.
- Indirect inheritance (the grandchild class) is also a subclass of the ancestor class.
Example 2: Using a Tuple of Classes
Example
# Check whether it belongs to multiple classes
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
class Robot:
pass
print(issubclass(Dog, (Animal, Robot))) # Output: True
print(issubclass(Dog, (Cat, Robot))) # Output: False
# A class is also a subclass of itself
print(issubclass(int, int)) # Output: True
print(issubclass(str, str)) # Output: True
# Abstract base class
from collections.abc import MutableSequence
print(issubclass(list, MutableSequence)) # Output: True
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
class Robot:
pass
print(issubclass(Dog, (Animal, Robot))) # Output: True
print(issubclass(Dog, (Cat, Robot))) # Output: False
# A class is also a subclass of itself
print(issubclass(int, int)) # Output: True
print(issubclass(str, str)) # Output: True
# Abstract base class
from collections.abc import MutableSequence
print(issubclass(list, MutableSequence)) # Output: True
Actual output:
Output the result according to the actual runtime conditions.
Code analysis:
- You can check whether it belongs to any one of multiple classes.
- A class is always a subclass of itself.
Example 3: Practical Application
Example
# Polymorphism implementation
class Shape:
def area(self):
raise NotImplementedError
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
# Check type
shapes = [Circle(5), Rectangle(3, 4), Circle(2)]
for shape in shapes:
if issubclass(type(shape), Shape):
print(f"Area: {shape.area()}")
# Use isinstance (recommended)
for shape in shapes:
if isinstance(shape, Shape):
print(f"Shape area: {shape.area()}")
class Shape:
def area(self):
raise NotImplementedError
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
# Check type
shapes = [Circle(5), Rectangle(3, 4), Circle(2)]
for shape in shapes:
if issubclass(type(shape), Shape):
print(f"Area: {shape.area()}")
# Use isinstance (recommended)
for shape in shapes:
if isinstance(shape, Shape):
print(f"Shape area: {shape.area()}")
Output:
Running in the actual environment will output the area of each shape.
Note: In actual programming, usually useisinstance()rather thanissubclass(type())。
Other Extensions
Python3 Built-in Functions