Search code examples
pythonsyntax-error

Is it possibile that Python interpreter makes a full syntax check before starting to execute code?


So far, I have understood that Python is an interpreted language so that the interpreter reads and executes instructions, one line at a time, and if it encouters an error, it stops execution and throws an exception. In other words, the execution goes on until an error occours and stops it but previous instructions have been executed. I saw that this behavior changes when there is a SyntaxError. Try this:

import random
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
print(random.randint(1,100))
l = [

The last line causes a SyntaxError. In this case no previous line of code is executed (no print in the console) and this pushed me to think that there can be a preliminary syntax check of the entire source code before execution starts. Could it be the reason of this behavior?


Solution

  • Python is not purely an "interpreted language". Just as Java and C# are not purely "compiled languages".

    These 3 languages (and many others), have both a compilation step, and the result of this is a bytecode output - which is them interpreted in a virtual machine. The major diference to later languages is that they are static languages: the compiled bytecode have all the information and slots for all data-types that will be held during program execution, while Python is a dynamic language: everything is an object, and only when trying to perform an operation in an object it is possible to know if the operation can be performed or not. What make a lot of people misperceive that is that the compilation step for Python is seamless: one just calls the execution of the .py source files, and the compilation step is not explicitly perceived, while in other languages one have to manually generate the intermediate bytecode files.

    This said, reiterating: Python will compile all source code in a file before executing it - and then it will execute the top level code - - learning and building functions and classes at execution time, and usually, ultimately, entering a function called explicitly as an entry point. A syntax problem will prevent the source file to be compiled in first place - there is where your error arises.

    (In an interactive environment, all input lines or blocks are compiled as <enter> is hit, and the resulting code incrementally executed in the same scope).

    Ad yes, I said it in this answer, and all logic and facts stand by it: despite large anedotic occurrence in the internet it is incorrect to say that Python is an interpreted language if you are contrasting this with languages that have a similar behavior in runtime. It is only partially truth, and that is the exact same behavior of a lot of languages deemed as "compiled".