Python inheritance judgment type

Judgment type isinstance(object, class)

class Person(object):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

class Student(Person):
    def __init__(self, name, gender, score):
        super(Student, self).__init__(name, gender)
        self.score = score

class Teacher(Person):
    def __init__(self, name, gender, course):
        super(Teacher, self).__init__(name, gender)
        self.course = course

p = Person('Tim', 'Male')
s = Student('Bob', 'Male', 88)
t = Teacher('Alice', 'Female', 'English')
print(isinstance(t, Person))
print(isinstance(t, Student))
print(isinstance(t, Teacher))
print(isinstance(t, object))

In the inheritance chain, an instance of a parent class cannot be a subclass type, because the subclass has more properties and methods than the parent class.
An instance can be seen as its own type, or as the type of its parent class.

Guess you like

Origin blog.csdn.net/angelsweet/article/details/114574726
Recommended