首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python程序是如何运行起来的

Python is not a precompiled language in theway that some other languages you may have come across are (such as C++).Instead it is what is known as an interpreted language (although even this isnot quite accurate). An interpreted language is one that does not require aseparate compilation phase to convert the human readable format into somethingthat can be excuted by a computer. Instead the plain text version is fed intoanother program (generally referred to as the interpreter) which then executesthe program for you.

Python 并不像您可能接触过的其他语言(如 C++)那样是一种预编译语言。相反,它是一种所谓的解释型语言(尽管这种说法也不太准确)。解释型语言不需要单独的编译阶段,就能将人类可读格式转换为计算机可执行的格式。相反,纯文本版本被输入到另一个程序(一般称为解释器)中,然后由解释器为你执行程序。

Python actually uses an intermediate modelin that it actually converts the plain text English style Python program intoan intermediate “pseudo” machine code format and it is this intermediate formatthat is executed. This is illustrated below:

Python 实际上使用的是一种中间模型,它实际上是将纯文本英文风格的 Python 程序转换成一种中间的 "伪 "机器码格式,而执行的正是这种中间格式。如下图所示:

The way in which the Python interpreterprocesses a Python program is broken down into several steps. The steps shownhere are illustrative (and simplified) but the general idea is correct(正确的).

Python 解释器处理 Python程序的方式分为几个步骤。这里显示的步骤是说明性的(简化的),但总体思路是正确的。

First the program is checked to make surethat it is valid Python, that is a check is made that the program follows allthe rules of the language and that each of the commands and operations, etc. isunderstood by the Python envionment.

首先对程序进行检查,以确保它是有效的 Python 程序,即检查程序是否遵循了语言的所有规则,以及 Python 环境是否能理解每个命令和操作等。

It then translates the plain text, Englishlike commands, into a more concise intermediate format that is easier toexecute on a computer. Python can store this intermediate version in a filewhich is named after the original file but with a “.pyc” extension instead of a“.py” extension (the ‘c’ in the extension indicates it contains the compiledversion of the code).

然后,它将纯文本(类似于英语命令)翻译成更简洁的中间格式,以便于在计算机上执行。Python可以将这种中间版本存储在一个文件中,文件名与原始文件相同,但扩展名为".pyc",而不是".py"(扩展名中的 "c "表示它包含代码的编译版本)。

The compiled intermediate(中间) version is then executed by the interpreter.

然后,解释器执行编译后的中间版本。

When this program is rerun the Pythoninterpreter checks to see if “.pyc” file is present. If no changes have beenmade to the source file since the “.pyc” was created, then the interpreter canskip steps 1 and 2 and immediately run the “.pyc” version of the program.

重新运行该程序时,Python 解释器会检查".pyc "文件是否存在。如果".pyc "文件创建后没有对源文件进行任何修改,那么解释器可以跳过步骤 1 和 2,立即运行程序的".pyc"版本。

One interesting aspect of Python’s usage isthat it can be (and often is) used in an interactive fashion, with individualcommands being entered and executed one at a time, with context informationbeing built up. This can be useful in debugging situations.

Python 使用的一个有趣方面是,它可以(而且经常)以交互方式使用,一次输入并执行一条单独的命令,并建立上下文信息。这在调试时非常有用。

  • 发表于:
  • 原文链接https://page.om.qq.com/page/Ox_9Q-AU4uF-XZT5jwKBUJ_A0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券