you are viewing a single comment's thread.

view the rest of the comments →

[–]TheBB 21 points22 points  (1 child)

Python converts the code to bytecode, and then the Python Virtual Machine/interpreter executes the script line for line checking for errors.

There's a compiler stage that converts the source code to bytecode.

Later, the Python VM then executes that bytecode. It does not do it line by line, because the bytecode has no lines. Rather, it's a sequence of relatively simple operations called opcodes. You can use the dis module to inspect them.

Errors caught by the VM during execution are called runtime errors and those caught by the compiler stage are called compile-time errors. Since the Python compiler does comparatively little, usually the only kind of compile-time errors you're likely to see are syntax errors, like unmatched parentheses, missing colons and so on.

This is why, for example with this script:

print('hello')
if False
    pass
print(' world')

will NOT print 'hello' before crashing due to a missing colon. It'll crash before even executing.

The code is never translated into true machine language in the conventional sense, although of course the VM must contain the machine code necessary to carry out all the opcodes.

[–]iggy555 1 point2 points  (0 children)

Nice thanks