Python Language Overview and Detailed operating mechanism

From now on, we will open the door to a new world of --Python programming language. Python is a cross-platform computer programming language. Is an object-oriented dynamically typed language , originally designed for writing automated scripts (shell), constantly updated with the addition of new features and language versions, is used more and more independent, large-scale development projects.

At the outset, Python series Bowen will be a large number of excellent reference books, websites and blog posts, bloggers be carefully sorted summarize and share it with everyone. Python blog series, including but not limited to the following sections:

  • Python basic grammar whole system : basic data types, data type combination, program control structure, function programming, file IO and text processing, data formatting, common modules, analytical and other classic title
  • Core Python Programming : high-level object-oriented programming, exception handling, regular expressions, network programming, multithreaded programming, GUI programming, database programming, etc.

Bowen any errors and inappropriate, please inform us, thank you to comment or private letter form!



Introduction to Python language

Python is an interpreted, object-oriented, dynamic data type of high-level programming language.

Python in late 1989 invented by Van Rossum Guido , the first public release was released in 1991. It was officially announced in 2020 January 1, stop the update of Python 2. Python 2.7 was identified as the last Python 2.x version. Python is now maintained by a core development team, Guido van Rossum continues to occupy a crucial role in guiding their progress.

Python is a combination of interpretive compiler, interactive object-oriented scripting language and high-level . Python is fully object-oriented language. Functions, modules, numbers, strings are objects. And fully supports inheritance, overloading, derived, multiple inheritance, enhance the beneficial reuse of source code. Python supports overloaded operators and the dynamic type.

Python's design highly readable, compared to other languages ​​often use English keywords, some of the punctuation other languages, it has more features than any other language grammar structure.

  • Python is an interpreted language: This means that the development process does not compile this link. Like PHP and Perl languages.
  • Python is an interactive language: This means that we can execute code directly after a Python >>> prompt.
  • Python is an object-oriented language: This means that Python supports object-oriented style of programming or code encapsulated in objects.

Features of the Python language

1, easy to learn, highly readable and beautiful syntax : Python follow the "simple, elegant, clear" design philosophy. In Python language, using indentation to identify code blocks, by reducing unnecessary braces, semicolons serve other visual noise, so that significantly improved readability. Good reading some of the same Python program feels almost like reading English, be able to focus on solving problems rather than entangled in the complicated grammar structure.

2, development of high efficiency : Python is a high-level language, C relative terms, at the expense of performance and enhance the efficiency of programmers. It allows the programmer can not focus on low-level details, and to focus entirely on the business logic implementation.

3, extensive standard library : Python language called its own battery, meaning Python language is very comprehensive class library, one of Python's greatest strength is its rich library.

4, a powerful third-party libraries, a wide range of applications : Python community provides a large number of third-party modules, used in a manner similar to the standard library. Function covers computer vision, scientific computing, artificial intelligence, machine learning, Web development, database interface, graphics system fields.

5, interactivity : interactive support, we can execute code from an input terminal and get the result, test, and debug code snippets interaction.

6. Portability : based on the characteristics of its open source, Python has been ported to many platforms.

7, scalability and can be embedded in nature : if we take a certain critical code quickly, or want to write some algorithms do not want to open, you can use C / C ++ to complete that part of the program, and then from a Python program transfer. Of course, we can also embed Python to C / C ++ program, allowing users to program the ability to obtain "script" of.

Database and GUI Programming : Python provides an interface to all major commercial databases; Python GUI support can be created and ported to many system calls.


Python language can not be ignored

1, running slow : Since Python is an interpreted language code in the execution of the program would translate into machine code line by line CPU can understand, this translation process is very time consuming, but because the C / C ++ language is a compiled language , the program compiled directly into machine code, so Python's speed compared to C / C ++ language does a lot slower. But in fact, referred to here running slow in most cases the user is unable to perceive directly, and requires the help of a professional test tools. So in fact, for the most part, Python is fully meet our requirements for the speed of the program, except for the preparation of high operating efficiency requirements of the program.

2, Python 2 is not compatible with Python3

3, the code can not be encrypted : For an interpreted language, publisher, is publishing the source code. For high confidentiality procedures, Python language does not fit.

4, multi-threaded multi-core can not take advantage of the CPU : Python threads are native operating system threads. On Linux is pthread, on Windows Win thread, executed entirely by the operating system thread scheduling. On multi-core CPU platform, because of the GIL, the prohibition of parallel execution of multiple threads. GIL Python is not characteristic, it is a concept at the time of implementation of the Python parser (CPython) introduced, i.e. GIL global interpreter lock (Global Interpreter Lock), the tool is a computer programming language interpreter is used to synchronize threads, such that any time only one thread in execution.


Detailed operating mechanism Python program

In-depth understanding 'interpretation and compilation'

We first look at an interpreted language and a compiled language .

The computer program, in fact, a set of computer instructions that can really drive the machine running the machine instructions , but let developers write directly ordinary machine instructions is not realistic, so there have been high-level computer language. High-level language allows the use of natural language (usually the English) programming, but high-level language program ultimately must be translated into machine instructions to execute.

Of course, the computer can not understand the high-level language, but can not directly execute high-level language, it can only be understood directly machine instructions , so use a program written in any high-level language if you want to be running computer must be converted into computer language, that is, machine instructions. And this conversion, there are two ways: compiled, interpreted . High-level language in a different computer implementation can be divided into two categories: static and scripting languages, the use of a static language compiler implementation, using a scripting language interpreted.

Compiled language prior to program execution, the process requires a specific compiler, the source code is compiled into a computer can execute machine code, the file can be run directly after compiling. Because compiled only once, do not need to run after the compile time, so the high efficiency compiled language . Such as C, C ++, Go, etc. to a compiled language.

Compiled language has the following characteristics:

  • For the target code execution speed faster same source code, compiled generated.
  • Related to a particular platform, generally not portable to other platforms, platform portability compiled language is not good.

Interpreted language using a dedicated interpreter of source code into machine code line by line explanation of a specific platform and executed immediately. It does not require pre-compiled, interpreted source code directly into machine code and executed immediately, so long as the internet provide a corresponding interpreter to run the program. Python, Php, etc. belong to an interpreted language, Python but not completely interpreted language, follow-up will be described in detail.

Interpreted language has the following characteristics:

  • Interpreted language each run needs to be interpreted as source code and machine code execution, less efficient.
  • As long as the appropriate interpreter platform, the source code can be run, so interpreted language internet better portability.
  • The need to retain interpreted source code, program debugging and maintenance is very convenient.

Interpreted and compiled difference is that once the translation is compiled, once the program is compiled, the compiler is no longer needed, or source code. Explained at each time are required runtime interpreter and source code.

The compilation process only once, therefore, the speed of the build process is not critical, the speed of the object code is the key . Therefore, the compiler usually integrated as much as possible optimization techniques, the target code generation with better efficiency. However, the interpreter can not integrate too many optimization techniques, because the code optimization techniques consume run time, the execution speed of the entire program is affected.

In simple terms, explain the implementation of the code written by one user is running, there is no overview of all the code performance optimization process, so the execution performance is slightly lower, but can support cross-operating system or hardware platform, the source code is very beneficial to retain the upgrade and maintenance, non-suit running performance-critical scenarios.

Python scripting language is a general-purpose high-level programming language widely used, although executed by way of explanation, but it's interpreter also retains some of the features of the compiler , as the program is running, the interpreter will generate a complete target code. This interpreter and compiler combination of new modern scripting language interpreter is a useful evolution in order to improve computer performance.


Python interpreter

Use a compiled program written in C / C ++, etc., is the need to convert from the source file into machine language used by the computer, after the linker links the formation of binary executable files. When you run the program, it can binaries loaded from the hard disk into memory and run.

However, for purposes of Python, Python need to compile the source code into binary code, which can be run directly from the program source code. Python interpreter converts the source code into byte code, and then forwards the compiled bytecode to Python virtual machine (the PVM) for execution. N When we run the program, Python interpreter will perform two steps:

1, the source code is compiled into bytecode . Bytecode compiler is a form specific to Python, it is not binary machine code, the compiler needs to be further executed by a machine, because like C / C ++ as fast as this is the Python code can not run. If the Python process has write privileges on the machine, it will save a byte code program is to .pyc for the file extension , if Python byte code can not be written on the machine, the bytecode will generating in memory and are automatically discarded at the end of the program. When building a program to best Python conferred on the authority to write on the computer, so long as the source code is not changed, the resulting file can be reused .pyc, improve the efficiency .

2, forwarding the compiled bytecode to Python virtual machine (the PVM) for execution . Python Virtual Machine PVM is short, it is the Python runtime engine, part of the Python system, which is a large loop iteration byte code instruction, one after another to complete the operation.

Therefore, Python is not entirely interpreted language, it is compiled , first .py source files compiled into .pyc or .pyo, and then by the Python virtual machine execution, py file it with respect, and compiled into .pyc and on .pyo .py no essential difference, but for this module loading speed increased, and did not improve the execution speed of the code.


Several versions of the Python interpreter implementation

There are three main Python interpreter implementations, CPython, Jython and IronPython the three ways.

CPython: CPython is the standard implementation , others are specific goals. CPython is written in C, which is most of the OS pre-installed Python interpreter, but also all the Python interpreter to run the fastest, most complete and robust .

Jython: Jython is an alternative implementation of the Python language, its purpose is to integrate with the Java programming language, Jython include Java classes that compile Python source code, Java bytecodes are formed, and the resulting byte code mapping to the Java virtual machine (JVM). Because Jython slower than CPython but also robust enough, it is often seen as an interesting tool for Java developers looking for Java code for a major front-end scripting language.

IronPython: IronPython purpose of design is to write Python programs can make on Linux and open source with the .NET framework on Windows platforms corresponding to the Mono application integration.

Published 219 original articles · won praise 1249 · Views 310,000 +

Guess you like

Origin blog.csdn.net/ZYZMZM_/article/details/103584738