简体   繁体   English

查询python执行模型

[英]Query on python execution model

Below is the program that defines a function within another function. 下面是在另一个函数中定义函数的程序。

在此输入图像描述

1) When we say python program.py Does every line of python source directly gets converted to set of machine instructions that get executed on processor? 1)当我们说python program.py每行python源是否直接转换为在处理器上执行的机器指令集?

2) Above diagram has GlobalFrame and LocalFrame and Objects. 2)上图包含GlobalFrame和LocalFrame以及Objects。 In the above program, Where does Frames Objects and code reside in runtime? 在上面的程序中, Frames Objectscode在运行时驻留在哪里? Is there a separate memory space given to this program within python interpreter's virtual memory address space? 在python解释器的虚拟内存地址空间中是否有一个单独的内存空间给这个程序?

"Does every line of python source directly gets converted to set of machine instructions that get executed on processor?" “是否每一行python源都会直接转换为在处理器上执行的一组机器指令?”

No. Python code (not necessarily by line) typically gets converted to an intermediate code which is then interpreted by what some call a "virtual machine" (confusingly, as VM means something completely different in other contexts, but ah well). 不可以。通常会将Python代码(不一定是行)转换为中间代码,然后由某些人称之为“虚拟机”来解释(令人困惑的是,因为VM意味着在其他环境中完全不同,但很好)。 CPython, the most popular implementation (which everybody thinks of as "python":-), uses its own bytecode and interpreter thereof. CPython,最流行的实现(每个人都认为是“python”:-),使用自己的字节码和解释器。 Jython uses Java bytecode and a JVM to run it. Jython使用Java字节码和JVM来运行它。 And so on. 等等。 PyPy, perhaps the most interesting implementation, can emit almost any sort of resulting code, including machine code -- but it's far from a line by line process!-) PyPy,也许是最有趣的实现,几乎可以发出任何类型的结果代码,包括机器代码 - 但它远非一行一行! - )

"Where does Frames Objects and code reside in runtime" “框架对象和代码驻留在运行时”

On the "heap", as defined by the malloc , or equivalent, in the C programming language in the CPython implementation (or Java for Jython, etc, etc). 在“堆”上,由malloc或等效的定义,在CPython实现中的C编程语言(或Java for Jython等)中。

That is, whenever a new PyObject is made (in CPython's internals), a malloc or equivalent happens and that object is forevermore referred via a pointer (a PyObject* , in C syntax). 也就是说,每当创建一个新的PyObject (在CPython的内部)时,就会发生malloc或等价物,并且该对象永远通过指针(C语法中的PyObject* )引用。 Functions, frames, code objects, and so forth, almost everything is an object in Python -- no special treatment, "everything is first-class"!-) 函数,框架,代码对象等等,几乎所有东西都是Python中的对象 - 没有特殊处理,“一切都是一流的”! - )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM