1

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?

1
  • A CPython code object references the bytecode, constants (including nested code objects), names, and execution info such as the flags and stack size. A frame object references the previous frame, code object, scoped namespaces (builtins, globals, locals), and instruction pointer. The frame of a regular function call uses an array of "fast locals", but module and class frames use a locals dict. Calling a function creates a frame from the arguments combined with its __code__, __globals__, __defaults__ , __kwdefaults__ (3.x), and __closure__. Commented Feb 1, 2015 at 6:51

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

In java, we say intermediate code as "byte code" that follows Vm specification. What about python? Oh so, code/frames/Objects are in heap?
CPython (which pre-dates Java by quite a while) also calls its intermediate code "byte-code" (though the specs are quite different from JVMs') - Jython does use JVM byte-code, Python.Net dotnet byte-code, etc, etc. And yes, just about everything in Python is an object, all first-class, all living in the heap after being malloced, etc, etc.
Can you please help me answer this query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.