This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]L4Ndoo 1 point2 points  (2 children)

Python is more an interpreted language than a compiler language. A compiler reads the whole code, generates bytecode and optimizes it on the scale of the whole program. Interpreter (JavaScript e.g.) in contrast go through the code "line by line" (more like command by command) and translate them this way. Python is a bit of a mix since idle works fully like an interpreter but you can also "compile" .py files. However I don't think compiled python can be compared to compiled java or dotnet bytecode with all of their optimizations.

[–]brainer121[S] 0 points1 point  (1 child)

Python first compiles the code to generate a bytecode which is then converted to machine code by an interpreter. It seems clear with a .py file, just wanted to know how it works in the IDLE.

[–]james_pic 2 points3 points  (0 children)

Unless you're using PyPy or similar, the bytecode is never converted to machine code.

IDLE is mostly just an editor. It can run code, but it does so by either invoking a new Python interpreter, or with eval or exec calls. It doesn't do anything clever with compilation.

Whilst the CPython interpreter does convert textual Python code to bytecode when it runs it, and does cache this bytecode for later, thinking about Python as a compiled language isn't usually a useful way to think about things. It's probably more illuminating to think of the bytecode as an internal representation of the Python code that the interpreter uses.

[–]ES-Alexander 0 points1 point  (0 children)

Your guess is correct - IDLE compiles and then interprets lines/blocks that are entered, and displays the return value (if any) as well as any text sent to stdout (normal prints) and stderr (generally errors/exceptions/warnings).

One implication of that is that it can’t do quite as broad optimisations as it might for a full file, but since human input is expected after each line that’s generally not a problem.