Home > Article > Backend Development > Detailed explanation explains the inheritance of Python classes in detail
What is inheritance in python:
New Classes do not have to be written from scratch
The new class inherits from the existing class and automatically has all the functions of the existing class
The new class only Need to write new features that are missing from existing classes
Benefits of inheritance:
Reuse existing code
Automatically possess all the functions of existing classes
Only need to write the missing new functions
Inherited features:
The subclass and the parent class are in an is relationship
Characteristics of python inheritance:
Always start from A class inherits
Don’t forget to call super().init
class Person(object): def init(self, name, gender): self.name = name self.gender = gender class Teacher(Person): def init(self, name, gender, course): super(Teacher, self).init(name, gender) self.course = course t = Teacher('Alice', 'Female', 'English') print t.name print t.course
The function isinstance() can determine the type of a variable. It can be used in Python’s built-in data types such as str, list, and dict, and can also be used in our Custom classes, they are essentially data types.
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 t = Teacher('Alice', 'Female', 'English') print isinstance(t, Person) print isinstance(t, Student) print isinstance(t, Teacher) print isinstance(t, object)
class Person(object): def init(self, name, gender): self.name = name self.gender = gender def whoAmI(self): return 'I am a Person, my name is %s' % self.name class Student(Person): def init(self, name, gender, score): super(Student, self).init(name, gender) self.score = score def whoAmI(self): return 'I am a Student, my name is %s' % self.name class Teacher(Person): def init(self, name, gender, course): super(Teacher, self).init(name, gender) self.course = course def whoAmI(self): return 'I am a Teacher, my name is %s' % self.name import json class Students(object): def read(self): return r'["Tim", "Bob", "Alice"]' s = Students() print json.load(s)
In addition to inheriting from one parent class, Python allows inheritance from multiple parent classes, called for multiple inheritance. Java cannot have multiple inheritance
class A(object): def init(self, a): print 'init A...' self.a = a class B(A): def init(self, a): super(B, self).init(a) print 'init B...' class C(A): def init(self, a): super(C, self).init(a) print 'init C...' class D(B, C): def init(self, a): super(D, self).init(a) print 'init D...' class Person(object): pass class Student(Person): pass class Teacher(Person): pass class SkillMixin(object): pass class BasketballMixin(SkillMixin): def skill(self): return 'basketball' class FootballMixin(SkillMixin): def skill(self): return 'football' class BStudent(BasketballMixin): pass class FTeacher(FootballMixin): pass s = BStudent() print s.skill() t = FTeacher() print t.skill()
In addition to using isinstance() to determine whether it is an instance of a certain type, is there any other way to obtain more What about the information?
First, you can use the type() function to get the type of the variable, which returns a Type object.
dir() function gets all the attributes of the variable.
The attributes returned by dir() It is a list of strings. If an attribute name is known, to get or set the attributes of the object, you need to use the getattr() and setattr() functions
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 def whoAmI(self): return 'I am a Student, my name is %s' % self.name print type(123) # <type 'int'> s = Student('Bob', 'Male', 88) print s # <class 'main.Student'> print dir(123) # ['abs', 'add', 'and', 'class', 'cmp', 'coerce', 'delattr', 'p', 'pmod', 'doc', 'float', 'floorp', 'format', 'getattribute', 'getnewargs', 'hash', 'hex', 'index', 'init', 'int', 'invert', 'long', 'lshift', 'mod', 'mul', 'neg', 'new', 'nonzero', 'oct', 'or', 'pos', 'pow', 'radd', 'rand', 'rp', 'rpmod', 'reduce', 'reduce_ex', 'repr', 'rfloorp', 'rlshift', 'rmod', 'rmul', 'ror', 'rpow', 'rrshift', 'rshift', 'rsub', 'rtruep', 'rxor', 'setattr', 'sizeof', 'str', 'sub', 'subclasshook', 'truep', 'trunc', 'xor', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real'] print dir(s) # ['class', 'delattr', 'dict', 'doc', 'format', 'getattribute', 'hash', 'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', 'gender', 'name', 'score', 'whoAmI'] print getattr(s, 'name') # Bob setattr(s, 'name', 'Adam') print s.name # Adam class Person(object): def init(self, name, gender, **kw): self.name = name self.gender = gender for k, v in kw.iteritems(): setattr(self, k, v) p = Person('Bob', 'Male', age=18, course='Python') print p.age # 18 print p.course #Python
The above is the detailed content of Detailed explanation explains the inheritance of Python classes in detail. For more information, please follow other related articles on the PHP Chinese website!