Questions that have plagued the world for years, whether Python is a language or a tool, Harvard professor's perfect interpretation

Source: Internet
Author: User

However, this is a very ambiguous sentence. What exactly does "Python" mean here? Is it an abstract interface for Python? is Python's generic implementation CPython (don't confuse CPython with Cython)? Or is it entirely something else? Maybe I'm referring to Jython, or IronPython, or pypy. Or instead of talking about Rpython or Rubypython (the two are completely different things).

The techniques mentioned above are often mentioned and referenced, and their purpose and scenario are completely different (at least, they operate in a completely different way).

Since I've worked with Python, I've used a variety of. *ython tools. But it wasn't until recently that I took the time to understand exactly what they were doing, how they worked, and why they were indispensable.

In this article, I will introduce the various implementations of Python and end with an introduction to PyPy, because I personally think it is the future of Python.

Everything starts with understanding what "Python" is.

If you're familiar with machine code, virtual machines, and so on, you can skip the start and start looking directly at the "Instant compile: PyPy and its future" section.

Is python an explanatory or a compile-type?

This is a question that a new python will confuse.

The first thing to understand is that Python is just an interface. There is a specific description of what Python should do and how to do it (just like any other interface), and there are a number of specific implementations (as well as other interfaces) that correspond.

The second thing to know is that "interpreted" and "compiled" are the characteristics of the implementation, not the characteristics of the interface.

So, the problem itself is not well organized.

Is python an explanatory or a compile-type? The problem is really not well organized.

For the most widely used implementations (CPython: implemented in C, usually simply as Python, if you don't know what I'm talking about, that's what you're using CPython), the answer to this question is: explanatory, but with some compile-style features. CPython the Python source code into bytecode, and then explain the bytecode, execute it.

* Note: This compilation is not compiled in the usual sense. Usually we say the compilation refers to the conversion of high-level language code into machine code. But the compilation here is actually a compilation in another sense. (translator, this sentence is not very understand, the original is it is a ' compilation ' of sorts, do not know what to do, ask you readers. )

Take a look at the answers above, which will help us understand some of the concepts that we'll cover later in this article.

BYTE code vs. machine code

It is important to understand the difference between bytecode and machine code (or native code), and perhaps the best approach is to look at an example:

C code is compiled into machine code and will be executed directly on the processor. Each instruction controls the CPU work.
Java code is compiled into bytecode and will be executed on the abstract computer of the Java Virtual Machine (JVM). Each instruction is handled by the JVM and the JVM interacts with the computer itself.
In short: machine code is much faster, but bytecode is easier to migrate and more secure.

The machine code changes with the machine, but the bytecode is the same on all machines. One might think that machine code is optimized for a particular environment.

Back to CPython, the tool chain is executed as follows:
CPython compile your Python source code to generate bytecode.
The bytecode is then executed on the CPython virtual machine.

Beginners often assume that Python is compiled because they see the. pyc file. This also has some justification: the. pyc file is formally followed by the bytecode file to be interpreted. So, if you've run your Python code before, and you've generated a. pyc file, it's much faster to run again because you don't need to compile the bytecode again.

Optional virtual machines: Jython,ironpython, etc.

As I mentioned earlier, Python has many implementations. As mentioned earlier, CPython is the most versatile. This is implemented in C and is considered a "default" implementation.

But what about the others? One of the most prominent of these is Jython, a Java implementation that uses the JVM. CPython generates bytecode that runs on the CPython virtual machine, and Jython generates Java bytecode that runs on the JVM (which is the same process as compiling Java programs that generate Java bytecode).


"Why do you use other implementations?" "You might be asking questions like that. Well, for developers, different implementations have different levels of support for various technical challenges.

It's easy to write C extensions for your Python code in CPython because the C interpreter is ultimately executed. Jython, on the other hand, makes it easy to work with other Java programs: Without additional work, you can import any Java class and use other Java classes in your Jython program. (Aside from that, if you don't really think about it, it's going to be hard.) At this point we are talking about mixing different language codes together and compiling them into the same program. (Rostin has been proposing mixed Fortran and C code programming for some time.) So, it's not new, but it's still cool. ))

Here is an example of a valid Jython code:

[Java HotSpot (TM) 64-bit Server VM (Apple Inc.)] On java1.6.0_51

>> from java.util import HashSet

>> s = HashSet (5)

>> S.add ("Foo")

>> S.add ("Bar")

>> s

[Foo, Bar]

IronPython is another popular Python implementation, implemented entirely in C #, against. NET platform. She runs in what can be called. NET virtual machine, this is Microsoft's Common Language Runtime (CLR), which corresponds to the JVM.

You might say, jython:java::ironpython:c#. Each of them runs on the same virtual machine, you can import C # classes from your IronPython, bring Java classes from your Jython code, and so on.

You can complete any work you do without any non-cpython implementation. But there are many benefits to using these technologies, most of which depend on the technology stack you are using now. Do you use a lot of JVM-based languages? Jython is prepared for you. It's all used. The language of the net world? Then you should try IronPython (maybe you're already using it)


By the way (although this is not a reason to use different implementations), note that Python's implementations are handled differently when dealing with your Python source. These differences are then very small, as these implementations are constantly evolving, and over time these differences are slowly converging and compatible. For example, IronPython uses Unicode strings by default, However, in the 2.x version of CPython, the default is an ASCII string (if a non-ASCII string is used, a unicodeencodeerror error is thrown), but in the 3.x version Cpythong has already supported the Unicode string by default.

Instant compilation: PyPy and its future

We already have a Python implementation written in C, one written in Java and one written in C #. Next is: Python implementation in Python (a person may notice that this is a problem, a dead loop, ^_^)

Next we see where it's easy to confuse. First, let's talk about the instant compiler JIT

JIT: Why do you have this? What is the principle of it?

We all know that the local machine code speed is much faster than the byte code. So what happens if we can compile some bytecode directly and then run it out of the machine code? We have to pay some price (such as time) to compile the bytecode to the local machine code, if the final run time is faster, then the cost is worth it. This is the motivation of the JIT compiler, a technique that mixes the benefits of interpreters and compilers. Simply put, JIT is trying to improve the speed of the Scripting interpreter system by compiling technology.

Like this article words, forwarding + comments Oh! Let's see your unique insights!

All right, give us this article on the Welfare Plus I QQ group: 836962007 can get Oh!

Questions that have plagued the world for years, whether Python is a language or a tool, Harvard professor's perfect interpretation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.