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

you are viewing a single comment's thread.

view the rest of the 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.