Python issubclass() 函数
issubclass() 是 Python 中用于检查类是否为另一个类的子类的内置函数。
issubclass() 用于判断类之间的继承关系,可以检查直接子类和间接子类。
单词释义: issubclass 是 is a subclass(是一个子类)的缩写。
基本语法与参数
语法格式
issubclass(class, classinfo)
参数说明
- 参数 class:
- 类型: 类
- 描述: 要检查的类。
- 参数 classinfo:
- 类型: 类或类元组
- 描述: 父类或要检查的类元组。
函数说明
- 返回值: 返回布尔值
True或False。 - 特点: 类总是被认为是自身的子类。
实例
示例 1:基础用法
实例
# 类定义
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
class Husky(Dog):
pass
# 检查继承关系
print(issubclass(Dog, Animal)) # 输出: True
print(issubclass(Cat, Animal)) # 输出: True
print(issubclass(Dog, Cat)) # 输出: False
# 间接继承
print(issubclass(Husky, Animal)) # 输出: True(继承链:Husky -> Dog -> Animal)
print(issubclass(Husky, Dog)) # 输出: True
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
class Husky(Dog):
pass
# 检查继承关系
print(issubclass(Dog, Animal)) # 输出: True
print(issubclass(Cat, Animal)) # 输出: True
print(issubclass(Dog, Cat)) # 输出: False
# 间接继承
print(issubclass(Husky, Animal)) # 输出: True(继承链:Husky -> Dog -> Animal)
print(issubclass(Husky, Dog)) # 输出: True
运行结果预期:
True True False True True
代码解析:
- 子类是父类的子类。
- 间接继承(孙子类)也是祖先类的子类。
示例 2:使用类元组
实例
# 检查是否属于多个类
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
class Robot:
pass
print(issubclass(Dog, (Animal, Robot))) # 输出: True
print(issubclass(Dog, (Cat, Robot))) # 输出: False
# 类自身也是自身的子类
print(issubclass(int, int)) # 输出: True
print(issubclass(str, str)) # 输出: True
# 抽象基类
from collections.abc import MutableSequence
print(issubclass(list, MutableSequence)) # 输出: True
class Animal:
pass
class Dog(Animal):
pass
class Cat(Animal):
pass
class Robot:
pass
print(issubclass(Dog, (Animal, Robot))) # 输出: True
print(issubclass(Dog, (Cat, Robot))) # 输出: False
# 类自身也是自身的子类
print(issubclass(int, int)) # 输出: True
print(issubclass(str, str)) # 输出: True
# 抽象基类
from collections.abc import MutableSequence
print(issubclass(list, MutableSequence)) # 输出: True
实际运行结果:
根据实际运行情况输出结果。
代码解析:
- 可以检查是否属于多个类中的任意一个。
- 类总是自身的子类。
示例 3:实际应用
实例
# 多态实现
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
# 检查类型
shapes = [Circle(5), Rectangle(3, 4), Circle(2)]
for shape in shapes:
if issubclass(type(shape), Shape):
print(f"面积: {shape.area()}")
# 使用 isinstance(推荐)
for shape in shapes:
if isinstance(shape, Shape):
print(f"形状面积: {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
# 检查类型
shapes = [Circle(5), Rectangle(3, 4), Circle(2)]
for shape in shapes:
if issubclass(type(shape), Shape):
print(f"面积: {shape.area()}")
# 使用 isinstance(推荐)
for shape in shapes:
if isinstance(shape, Shape):
print(f"形状面积: {shape.area()}")
运行结果:
在实际环境中运行会输出各形状的面积。
注意:实际编程中通常使用 isinstance() 而非 issubclass(type())。
Python3 内置函数
点我分享笔记