overexchange
overexchange

Reputation: 1

Query on python execution model

Below is the program that defines a function within another function.

enter image description here

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?

2) Above diagram has GlobalFrame and LocalFrame and Objects. In the above program, Where does Frames Objects and code reside in runtime? Is there a separate memory space given to this program within python interpreter's virtual memory address space?

Upvotes: 1

Views: 510

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881675

"Does every line of python source directly gets converted to set of machine instructions that get executed on processor?"

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). CPython, the most popular implementation (which everybody thinks of as "python":-), uses its own bytecode and interpreter thereof. Jython uses Java bytecode and a JVM to run it. 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!-)

"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).

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). Functions, frames, code objects, and so forth, almost everything is an object in Python -- no special treatment, "everything is first-class"!-)

Upvotes: 3

Related Questions