In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the inheritance of Python class". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the inheritance of Python class".
1. Understanding of inheritance
Inheritance: a high-level abstraction of code reuse
Inheritance is one of the quintessence of object-oriented design
High-level abstraction level code reuse based on classes is realized.
Inheritance is a process in which a newly defined class can almost completely use the properties and methods of the original class.
Whether it's a base class or a derived class, it's just an inheritance, and it's a normal Python class.
It can also be divided by subclass, parent class, and superclass.
The most basic class is the base class. After one inheritance, the derived class can be inherited again and a derived class can be obtained again. Now the most basic class and the first inherited derived class are the relationship between the parent class and the subclass. The last derived class of the derived class is also the relationship between the parent class and the subclass, while the most basic class and the last derived class belong to the relationship between the superclass and the subclass.
Derived classes can inherit not only one base class, but also multiple base classes. This is the concept of multi-inheritance.
2. Construction of class inheritance
The inheritance relationship is declared at the time of definition when the class inherits, and the syntax structure is as follows
Class (): # Base class name can take the path: ModuleNama.BaseClassName def _ _ init__ (self,):...
Derived classes can directly use the properties and methods of the base class
The properties of the base class are basically equal to those defined in the derived class.
Derived classes can directly use the class properties and instance properties of the base class.
Derived classes can directly use various methods of the base class
When using the class methods and properties of the base class, use the class name of the base class
Example code:
Class TestClass: def _ _ init__ (self Number): self.sum_number = 0 for i in range (number + 1): self.sum_number + = I def sum_num (self): return self.sum_numberclass HumanNameClass (TestClass): def double_sum (self): return self.sum_number * 2 # use of base class attributes value1 = HumanNameClass (100) print (value1.sum_num ()) # 5050 # pair Use of base class instance methods print (value1.double_sum ()) # 10100 # use of derived class instance methods
There are two functions related to the judgment of inheritance relationship in Python
Connect to the above code
Print (isinstance (value1, TestClass)) # Trueprint (isinstance (value1, HumanNameClass)) # Trueprint (isinstance (value1, int)) # Falseprint (issubclass (HumanNameClass, TestClass)) # Trueprint (issubclass (TestClass, HumanNameClass)) # the most basic class in False3, Python
Because everything in Python is an object, any class is also an object, and all data types of Python are also objects; the most basic class that the Python language provides for all classes is object.
Object is the most basic noun of Python and does not need translation.
Inherit object by default when all are defined
Reserved properties and methods are essentially properties and methods of the object class
Sample code:
Print (object.__name__) # print the name of the object # objectprint (object.__bases__) # print the class name inherited by the object # () print (object.__doc__) # print the object class description # The most base typeprint (object.__module__) # print the name of the module where the object is located # builtinsprint (object.__class__) # the class information corresponding to the object #
There are three elements of the Python object:
Identify identity: once the object is built, it will not change. It will be obtained with id (), usually the memory address.
Type type: the type of the object, obtained with type ()
Value value: divided into two types: variable mutable and immutable immutable
Two Python built-in functions related to basic classes
The function / keyword describes that id (x) returns the identity of x. The id () function in CPython is used to get the memory address of the object. X is y judges whether the marks of x and y are equal, returns True or False, and does not judge the value 4, the overload of the Ython class
Overloading is the definition of a base class property or method by a derived class
Property overloading: a derived class defines and uses a property with the same name as the base class
Method overloading: a derived class defines and uses a method with the same name as the base class
4.1 attribute overload
Attribute overload adopts the principle of nearest override, and no special tag is needed for attribute overloading. Methods and steps
Give priority to properties and methods that are redefined by derived classes
Then look for the properties and methods of the base class
Looking for superclass properties and methods
Example code:
Class TestClass: text = "this is the class attribute of the base class" def _ _ init__ (self, number): self.sum_number = 0 for i in range (number + 1): self.sum_number + = I def sum_num (self): return self.sum_numberclass HumanNameClass (TestClass): text = "this is the class attribute of the derived class" # Class attribute overloads def _ init__ (self Number): self.sum_number = 1000 # instance property overload def double_sum (self): return self.sum_number * 2value1 = HumanNameClass (100) print (TestClass.text) # this is the base class property print (value1.text) # this is the derived class property print (value1.sum_num ()) # 10004.2 method overload
Method overloading is the definition of base class methods by derived classes, which is divided into full overloading and incremental overloading.
Full overloading: derived classes completely redefine methods with the same name as the base class
You can define a method with the same name directly in a derived class
Sample code:
Class TestClass: def _ init__ (self, number): self.sum_number = 0 for i in range (number + 1): self.sum_number + = I def sum_num (self): return self.sum_numberclass HumanNameClass (TestClass): def sum_num (self): reconstruction of # method return self.sum_number * 2value1 = HumanNameClass (100) print (value1.sum_num ()) # 10100
Incremental overloading: derived class extensions define methods with the same name as the base class, syntax structures
Class (): def (self,): super ().
Incremental overloading uses the super () function
Sample code:
Class TestClass: def test_text (self): print ("this is the method of the base class") class TestClass1 (TestClass): def test_text (self): # incremental overloading super (). Test_text () print ("this is the statement in the new method") doc1 = TestClass () doc2 = TestClass1 () print (doc1.test_text () print (doc2.test_text ())''- -this is the base class method None # because the function does not return a value, this is the base class method, this is the statement None # in the new method, because the function does not return the value''5, Multiple inheritance of a class
The construction of multi-inheritance is to declare the inheritance relationship and syntax structure at the time of definition.
Class (,...,): # Base class name can take the path: ModuleNama.BaseClassName def _ init__ (self,):...
The method of depth first, from left to right, is adopted for most inheritance in Python. The so-called depth priority from left to right is to start from the far left to find his base class. If the base class is not looking above, it is not until the most basic object class has not been found that the search begins to the right.
All properties and methods are selected according to the method of "depth first from left to right".
The constructor also refers to the above principles, and super () also refers to the above principles.
The order of multiple base classes is the key.
Sample code:
Class Test1: def test (self): text = "this is base class 1" return textclass Test2: def test (self): text = "this is base class 2" return textclass Test3 (Test1, Test2): passclass Test4 (Test2, Test1): passvalue1 = Test3 () value2 = Test4 () print (value1.test ()) # this is base class 1print (value2.test ()) # this is base class 2
Determine the outcome of the output sequentially
Understanding of the concept of inheritance, the construction of class inheritance, understanding that object is the most basic class in Python
The overloading principle of class attributes is the nearest override principle.
Overloading of class methods: reloading is similar to overloading of class properties; incremental overloading uses the super () function
The method of multi-inheritance is depth first, from left to right.
Thank you for your reading, the above is the content of "how to understand the inheritance of the Python class", after the study of this article, I believe you have a deeper understanding of how to understand the inheritance of the Python class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.