What is the easiest way to learn Python?
As a programmer or quasi-programmer, you can't be familiar with object-oriented programming. As one of the most popular programming ideas today (perhaps you can get rid of "one"), object-oriented is an unavoidable topic in both interviews and work.
For Python programmers, the three major features of OOP (Object-Oriented Programming)—data encapsulation, inheritance, and polymorphism—are often the focus of an interview, so most people are familiar with it.
However, do you really understand the advantages and disadvantages of OOP? Today this article will lead you to understand the advantages and disadvantages of the three characteristics of inheritance.
class
OOP () is called object-oriented programming, is a programming idea. OOP uses objects as the basic unit of a program, and an object contains functions for data and operational data. Object-oriented programming treats a computer program as a collection of objects, each of which can receive messages from other objects and process them. The execution of a computer program is a series of messages passed between objects.
The most important concepts of object-oriented are the class and the instance. It must be borne in mind that the class is an abstract template, and the instance is a concrete "object" created according to the class. Each object has the same method. However, the respective data may be different.
Suppose we want to create a Student class. In Python, the class is defined by the class keyword:
The class is followed by the class name, that is, Student, the class name is usually the word at the beginning of the capitalization, followed by (object), indicating which class the class is inherited from. The concept of inheritance will be discussed later, usually, if not The appropriate inheritance class uses the object class, which is the class that all classes will eventually inherit.
After defining the Student class, you can create an instance of Student based on the Student class. The created instance is implemented by the class name +():
As you can see, the variable bart points to an instance of Student. The following 0x10a67a590 is the memory address. The address of each object is different, and the Student itself is a class.
You can freely bind an instance variable to a property, for example, to bind a name property to the instance bart:
Since classes can act as templates, you can force some attributes that we think must be bound when creating an instance. By defining a special __init__ method, when creating an instance, bind the attributes such as name and score:
Note: There are two underscores before and after the special method "__init__"! ! !
Note that the first argument to the __init__ method is always self, which means that the instance itself is created. Therefore, inside the __init__ method, you can bind various properties to self, because self points to the created instance itself.
With the __init__ method, when creating an instance, you can't pass in an empty parameter. You must pass in a parameter that matches the __init__ method, but you don't need to pass it. The Python interpreter will pass the instance variable yourself. :
Compared with ordinary functions, the function defined in the class is only a little different, that is, the first parameter is always the instance variable self, and, when called, the parameter is not passed. In addition, the methods of the class are no different from ordinary functions, so you can still use default parameters, variable parameters, keyword arguments, and named keyword arguments.
inherit
What is inheritance?
Inheritance is a way to create a class. In Python, a class can inherit from one or more parent classes. The original class is called the base class or super class.
View inheritance:
When do you use inheritance?
If there are already several classes, and there are common variable attributes and function attributes between the classes, then you can extract these variable attributes and function attributes as the attributes of the base class. The special variable attributes and function attributes are defined in this class, so that only the base class needs to be inherited, and the variable attributes and function attributes of the base class can be accessed. Can improve the scalability of the code.
Inheritance and abstraction (first abstraction and then inheritance)
Abstraction extracts similar parts. A base class is a class obtained by abstracting properties common to multiple classes.
Both the Garen and Riven classes have nickname, aggressivity, life_value, script four variable properties and the attack() function property. Here you can abstract a Hero class, which contains these properties.
Strictly speaking, the above Hero.init(self,...) cannot be counted as a method for a subclass to call a parent class. Because if we remove the inheritance relationship (Hero), the code can still get the expected results.
Summarize the characteristics of inheritance in python:
In a subclass, the base class's init() is not automatically called and needs to be called manually in the derived class.
When calling the methods of the base class, you need to add the class name prefix of the base class, and you need to take the self parameter variable.
First find the method called in this class, can not find it to find in the base class.
Discussion on the advantages and disadvantages of inheritance
Subclassing the disadvantages of built-in types
1. The method of the built-in type does not call the method of subclass coverage.
Built-in classes can be subclassed, but methods of built-in types do not call methods covered by subclasses. The following is an example of rewriting __setitem__ with a custom subclass that inherits dict:
As you can see from the output, the __setitem__ of dict is called when the key value is stored in a for one=1 and three=3. Only the [] operator will call the method we pre-covered.
The solution to the problem is not to subclass dict, but subclass colections.UserDict.
2, subclass class in the collections
User-defined classes should inherit collections such as UserDict, UserList, and UserString. These classes are specially designed so they are easy to expand. The code for subclassing UserDict is as follows:
Summary: The above problem only occurs in the case of the built-in type subclassing of the C implementation, and only affects custom classes that directly inherit the built-in types. Conversely, subclassing classes written in Python, such as UserDict or MutableMapping, will not have this problem.
Multiple inheritance
1. Method Resolution Order (MRO)
In multiple inheritance, there is a conflict problem caused by an unrelated ancestor class implementing the method of the same name. This problem is called a "diamond problem." Python relies on a specific order to traverse the inheritance graph, which is called the method parsing order. As shown in the figure, the left picture is the UML diagram of the class, and the dotted arrow in the right figure is the method resolution order:
2, super
Referring to the class attribute __mro__, you will mention super:
Super is a class that is neither a keyword nor a function or other data structure.
Role: super is a subclass used to call the parent class method.
Syntax: super(a_type, obj);
A_type is __mro__ of obj, of course, it can also be part of __mro__, while issubclass(obj,a_type)==true
For example, there is an MRO: [A, B, C, D, E, object]
We call this: super(C, A).foo()
Super will only look after C, ie: will only look for foo methods in D or E or object.
The following constructs a multiple inheritance of the diamond problem to deepen the understanding:
The output is as follows:
Analysis: d.pingpong() executes super.ping(), super finds the ping method of the parent class according to MRO, and queries B.ping() after class B to ping.
3. Suggestions for handling multiple inheritance
(1) Separating interface inheritance from implementation inheritance;
Inheritance interface: create subtypes, which are the pillars of the framework;
Inheritance implementation: By avoiding code duplication by reuse, you can usually switch to combination and delegate mode.
(2) using an abstract base class to explicitly represent the interface;
(3) By mixing the reuse code; the mixed class provides method implementation for multiple unrelated subclasses, which is easy to reuse, but will not be instantiated. And concrete classes can't just inherit mixed classes.
(4) Clearly indicate the mix in the name; Python does not declare the class as a regular way of mixing, Luciano recommends adding the Mixin suffix to the name. For example, XView in Tkinter should become XViewMixin.
(5) The abstract base class can be used as a mix, and vice versa; the similarities and differences between the abstract base class and the mix:
Abstract base classes define types, which can't be mixed;
Abstract base classes can be used as the only base class for other classes, and can't be mixed;
The concrete method of abstract base class implementation can only cooperate with the methods in the abstract base class and its super class. There is no such limitation in the mix.
(6) Do not subclass multiple concrete classes; concrete classes may not have, or at most one specific superclass. For example, in Class Dish (China, Japan, Tofu), if Tofu is a concrete class, then China and Japan must be abstract base classes or mixed.
(7) Provide the user with an aggregation class; the aggregation class means that the structure of a class is mainly inherited from the mix, and there is no added structure or behavior. Tkinter adopted this recommendation.
(8) Prioritize the use of object combinations instead of class inheritance. Prioritizing combinations can make the design more flexible. Combinations and delegates can be used instead of blending, but they cannot replace interface inheritance to define type hierarchies.
ZGAR AZ MC Disposable
ZGAR electronic cigarette uses high-tech R&D, food grade disposable pod device and high-quality raw material. All package designs are Original IP. Our designer team is from Hong Kong. We have very high requirements for product quality, flavors taste and packaging design. The E-liquid is imported, materials are food grade, and assembly plant is medical-grade dust-free workshops.
Our products include disposable e-cigarettes, rechargeable e-cigarettes, rechargreable disposable vape pen, and various of flavors of cigarette cartridges. From 600puffs to 5000puffs, ZGAR bar Disposable offer high-tech R&D, E-cigarette improves battery capacity, We offer various of flavors and support customization. And printing designs can be customized. We have our own professional team and competitive quotations for any OEM or ODM works.
We supply OEM rechargeable disposable vape pen,OEM disposable electronic cigarette,ODM disposable vape pen,ODM disposable electronic cigarette,OEM/ODM vape pen e-cigarette,OEM/ODM atomizer device.
ZGAR AZ MC Vape,ZGAR AZ MC Vape disposable electronic cigarette,ZGAR AZ MC vape pen atomizer ,AZ MC E-cig,AZ CC Vape disposable e-cigarette
Zgar International (M) SDN BHD , https://www.zgarecigarette.com