Home > Backend Development > Python Tutorial > Why is Python cross-platform?

Why is Python cross-platform?

(*-*)浩
Release: 2020-09-16 14:07:50
Original
14996 people have browsed it

The reason why python can be cross-platform: the source code is cross-platform; Python instructions will eventually be converted into a c language instruction set, and then the c language instruction set will be compiled into machine code and finally run. Cross-platform is limited: 1. There cannot be anything special for Windows; 2. The encoding method must be compatible.

Why is Python cross-platform?

Cross-platform concept is an important concept in software development, that is, it does not depend on the operating system or the hardware environment.

An application developed under one operating system can still run under another operating system. Relatively speaking, if a certain computer language can be highly cross-platform without modifying the code, then the language will be more abstract and the hardware control will be lower, and it will only be suitable for developing highly abstract model systems. For example, Java, Delphi, Yi Language, and Python are all cross-platform. They will be able to be developed, run and maintained under a variety of systems.

Most computer languages ​​are cross-platform in an absolute sense: because they issue instructions to the CPU in a high-level, human-readable way, there is no need to rely on any operations. system. But if you want to use the system's component toolbox to create a new graphical user interface (GUI), you may use API functions or library classes in the developer's specific system. Although C is cross-platform, C programs that use the Win32 API under Windows generally cannot be compiled on Unix machines. Different compilers also interpret language specifications differently. In this case, the program needs to be considered before building for different systems.

Some languages ​​such as Java have realized from the beginning that they need to run on various platforms, so cross-platform has been implemented in the local language environment of their platforms. For example, Java can be used cross-platform precisely because the Swing library is implemented on many platforms. Similarly, cross-platform file access is possible because there are libraries for file access under respective platforms. By analogy, various cross-platform problems require their own local libraries to solve. The wxWidgets framework is such a cross-platform library that provides many different solutions according to different cross-platform problems; there are many similar libraries, and corresponding libraries can be used according to cross-platform development in different languages.

The feasibility of providing and testing respective compiled versions for each operating system and CPU is very small; open source software allows users to compile the object code themselves, so that cross-border The platform aspect is better. Similarly, those interpreted languages, or languages ​​that require virtual machines, are more in line with cross-platform requirements, because users also have to compile them themselves. Sun's Java virtual machine Hotspot only provides compiled binary files for a few but not all platforms. For example, Sun only supports the i386 platform for GNU/Linux, but if you run Linux on a PowerPC or SPARC computer, you have to compile local machine code yourself or use third-party software to run Java programs.

Many APIs (Application Programming Interfaces) are platform dependent. OpenGL can be considered cross-platform because it does not depend on any specific operating system, CPU architecture, or brand of graphics device. Platform-specific APIs can be created as compatibility layers on other systems, such as WINE libraries, so that Windows programs can run on UNIX systems.

In addition, many programming languages ​​​​have cross-platform extensions and middleware, so that programmers can compile/run on different platforms with only a few minor modifications to the same source code, such as Qt and wxWidgets. .

What can be done in Python across platforms?

Because Python finally converts these own instructions into a C language instruction set, and then compiles the C language instruction set into machine code and then runs it. This is why Python's performance is so good The reason for the difference (of course, there are many ways to speed up).

Of course there are limitations:

1. There cannot be anything special for windows;

2. The encoding method must be compatible.

Supports common mainstream platforms, such as AIX, HPUX, Solaris, Linux, Windows, etc. In addition to Windows, common Unix and Linux platforms have native Python, but the version is generally lower. Regarding cross-platform, just like other cross-platform languages, it should be noted that some individual modules are unique to a single platform, but the overall cross-platform performance is still very good. There is no need to write multiple sets of code to adapt to multiple platforms.

But this does not mean that there are no restrictions at all: first, the intermediate files .py, .pyc and .pyo of the same version are cross-platform; secondly, PC and mobile terminals, such as mobile phones and Pads cannot be cross-platform. Platform (see the next item for the reason); finally, it cannot cross processor architectures, such as Intel and ARM, 64-bit and 32-bit.

The above is the detailed content of Why is Python cross-platform?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template