Python中的issubclass()示例
时间:2020-01-09 10:44:19 来源:igfitidea点击:
Python的issubclass()是一个内置函数,用于检查传递的类是否是指定的另一个类的子类。
Python issubclass()的语法如下:
issubclass(class,classinfo)
如果class是classinfo的子类(直接,间接或者虚拟),则函数返回True。
classinfo可以是一个类,也可以是一个类对象的元组,如果它是一个类对象的元组,则将对元组中的每个条目检查class(第一个参数)。
Python issubclass()示例
1.很明显,在Python中使用继承时,将使用issubclass()函数。这是一个层次结构,其中Employee类从Person继承,而Manager类从Employee继承,因此这里有多级继承方案。让我们使用issubclass()检查不同的组合。
class Person: def __init__(self, name, age): self.name = name self.age = age def display_info(self): print('Name:', self.name) print('Age:', self.age) class Employee(Person): def __init__(self, emp_id, department, name, age): super().__init__(name, age) self.emp_id = emp_id self.department = department def display_info(self): super().display_info() print('Id:', self.emp_id) print('Department:', self.department) class Manager(Employee): def __init__(self, perks, emp_id, department, name, age): super().__init__(emp_id, department, name, age) self.perks = perks def display_info(self): super().display_info() print('Perks:', self.perks) print("issubclass(Employee, Person):", issubclass(Employee, Person)) print("issubclass(Manager, Person):", issubclass(Manager, Person)) print("issubclass(Manager, Employee):", issubclass(Manager, Employee)) print("issubclass(Employee, Manager):", issubclass(Employee, Manager)) print("issubclass(Person, Employee):", issubclass(Person, Employee))
输出:
issubclass(Employee, Person): True issubclass(Manager, Person): True issubclass(Manager, Employee): True issubclass(Employee, Manager): False issubclass(Person, Employee): False
Manager也间接地从Person继承而来,所以它也是Person类型的,因此issubclass(Manager,Person)返回true。反之,虽然Manager不是Employee的子类,也不是True,Person也不是Employee的子类,所以这两个函数调用都返回false。
2.在此示例中,我们将在issubclass()函数中将元组作为第二个参数传递。
class Person: def __init__(self, name, age): self.name = name self.age = age def display_info(self): print('Name:', self.name) print('Age:', self.age) class Employee(Person): def __init__(self, emp_id, department, name, age): super().__init__(name, age) self.emp_id = emp_id self.department = department def display_info(self): super().display_info() print('Id:', self.emp_id) print('Department:', self.department) class Manager(Employee): def __init__(self, perks, emp_id, department, name, age): super().__init__(emp_id, department, name, age) self.perks = perks def display_info(self): super().display_info() print('Perks:', self.perks) print("issubclass(Employee, (str, list))):", issubclass(Employee, (str, list))) print("issubclass(Employee, (str, list, Person)):", issubclass(Employee, (str, list, Person))) print("issubclass(Manager, (str, list)):", issubclass(Manager, Employee)) print("issubclass(Person, (Employee, Manager)):", issubclass(Person, (Employee, Manager))) print("issubclass(Person, (Employee, Manager, object)):", issubclass(Person, (Employee, Manager, object)))
输出:
issubclass(Employee, (str, list))): False issubclass(Employee, (str, list, Person)): True issubclass(Manager, (str, list)): True issubclass(Person, (Employee, Manager)): False issubclass(Person, (Employee, Manager, object)): True
由于Employee不是str或者list的子类,因此第一次调用显然返回false。
第二个是正确的,因为Employee是Person的子类,并且元组中的一项通过了检查。
最后一次调用issubclass(Person,(Employee,Manager,object))返回true,因为object是Python中所有类的超类。