This chapter mainly introduces python Language related knowledge , Including its language type , As object oriented 3 Big features and 5 General principles , At the end, the advantages and disadvantages are introduced .
<>python It's an interpretive scripting language
<> Interpretive language
And C/C++ Compared with other compiled languages ,python Language programs are not first compiled into binary machine code and then run , Instead, it was created by python The parser interprets a sentence and displays it in the python Running in virtual machine , Until the end of the program or an error is thrown . In this way, program developers don't have to pay too much attention to library links , Compilation errors and other issues , This is done by the interpreter and the virtual machine , Greatly reduced the development time .
<> scripting language
Script language is also called dynamic language , There is no need to compile the program in advance , It can be deployed dynamically , That is, input a sentence in the console to run a sentence . This kind of dynamic property simplifies to a certain extent “ development , Deployment and debugging ” Time of .
<>python Is an object-oriented high-level language
<> Properties and methods
stay python in , Everything is an object , Like a function , A class , Even a module , A file . stay python in , An object is generally composed of attributes and methods , Property is the variable that the object contains , Method to implement the operation on the object , For example, increase , Delete , check , change . For example, an object that implements a circle , Its properties include circular coordinates and radius , The method is to create a circle （ initialization ）, Change the radius or circle , Delete the circle , Or draw an image on the interface .
<> Three characteristics of object oriented
Object oriented programming language , There must be packaging , Three characteristics of polymorphism and inheritance
So called encapsulation , That is to encapsulate the objective things into abstract classes , And classes can only let trusted classes or objects operate on their own data and methods , Information hiding for untrusted . Encapsulation is one of the characteristics of object-oriented , Is the main feature of object and class concepts . In short , A class is a logical entity that encapsulates data and the code that operates on it . Inside an object , Some code or some data can be private , Not accessible to the outside world . In this way , Objects provide different levels of protection for internal data , In order to prevent the irrelevant part of the program from accidentally changing or mistakenly using the private part of the object . stay python in , By default, all properties are public , To make it private , You need to add __( Two underscores ), for example __time.
Simple understanding of polymorphism , That is, the object can be different according to the input , Respond to different outputs . Formally speaking, the same method of a class instance has different forms in different situations . Polymorphism mechanism enables objects with different internal structures to share the same external interface . This means that , Although the specific operations for different objects are different , But through a common class , they （ Those operations ） It can be called in the same way .
Inheritance is a method that allows an object of one type to obtain the properties of an object of another type . It supports the concept of classification by level . Inheritance refers to such ability ： It can use all the functions of an existing class , These functions can be extended without rewriting the original classes . A new class created by inheritance is called “ Subclass ” or “ Derived classes ”, The inherited class is called “ Base class ”,“ Superclass ” or “ Superclass ”. The process of inheritance , It's the process from general to special . To achieve inheritance , It can be done through “ inherit ”（Inheritance） and “ combination ”（Composition） To achieve . There are two ways to realize the concept of inheritance ： Implementation inheritance and interface inheritance . Implementing inheritance refers to the ability to directly use the properties and methods of the base class without additional coding ; Interface inheritance refers to using only the names of properties and methods , But subclasses must provide implementation capabilities ;
<> Object oriented 5 General principles
<> Single responsibility principle SRP(Single Responsibility Principle)
It means that the function of a class should be single , It can't be all inclusive . Just like a person , Don't assign too much work , Otherwise, although busy all day long , But efficiency is not high .
<> Open and closed principle OCP(Open－Close Principle)
A module should be open in terms of extensibility and closed in terms of changeability . such as ： A network module , The original only server-side functions , Now, add the client function ,
Then it should be in the premise of not modifying the server-side function code , Can increase the implementation code of client function , This is required at the beginning of the design , The server and the client should be separated , The public part is abstracted .
<> Substitution principle (the Liskov Substitution Principle LSP)
A subclass should be able to replace the parent class and appear wherever the parent can appear . such as ： The company has an annual party , All employees can participate in the lottery , So whether it's an old employee or a new employee ,
Whether it's headquarters staff or expatriate staff , Should be able to enter the lottery , Otherwise, the company will be out of harmony .
<> The principle of dependence (the Dependency Inversion Principle DIP)
Concrete depends on abstraction , The upper layer depends on the lower layer . hypothesis B It's a comparison A Low module , but B Need to use A Function of ,
This is the time ,B It should not be used directly A Concrete classes in ： It should be B Define an abstract interface , And by A To implement this abstract interface ,B Only use this abstract interface ： And that's how it's done
The purpose of the inversion of dependence ,B It's gone. Yeah A The dependence of , The reverse is true A Rely on B Defined abstract interface . It is difficult to avoid relying on the lower module through the upper module , If B Also directly dependent on A Implementation of , Then it may cause circular dependence . A common problem is compiling A Module needs to be directly included in the B Modular cpp file , And compiling B It should also be directly included in the A Of cpp file .
<> Interface separation principle (the Interface Segregation Principle ISP)
Modules should be separated by abstract interface , Instead of being strongly coupled through concrete classes .
<>python Advantages and disadvantages of
<> The syntax and data structure are simple
python The grammar of English is more in line with human habits , So when I read the code, I have a feeling of reading English .python The essence of is pseudo code , The original intention is that developers can focus on the problem itself without being limited by the programming language .
python There are 3 species ： sequence （ list , tuple , character string ）, Dictionaries （ mapping ）, aggregate . Operations for each data structure （ Add, delete, check and modify ） The grammar is almost the same , Very convenient .
<> Format specification
python Forced indent is used as program interval , The general indent is 4 Spaces . The advantage is that the code is easy to read and very concise .
<>python It's an open source language
because python Open source , You can easily read the source code , And release software freely .
<> Extensibility and Embeddedness
If you need a piece of your critical code to run faster or want some algorithms to be hidden , You can use some of your programs C or C
to write , And then in your Python Use them in the program . You can put Python Embed your C/C program , To provide scripting capabilities to your program users .
<> Rich third party Library
python There are a lot of libraries for , There are very effective libraries in almost every field , These libraries bring great convenience to developers .
<> The running speed is slightly slow
be relative to C Equivalent compiler language ,python At running speed （ Especially the calculation speed ） A little slower , But it works C Rewrite part of the code for a large number of calculations , Reuse python call , It can increase speed .