1

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?

  • 2
    "One line at a time" only applies at the interactive Python prompt. When running a script file, the entire file is compiled before anything gets executed. – jasonharper Jun 22 '23 at 16:43
  • Does this answer your question? [Is Python interpreted, or compiled, or both?](https://stackoverflow.com/questions/6889747/is-python-interpreted-or-compiled-or-both) – slothrop Jun 22 '23 at 16:45
  • This is also the kind of thing that [linters](https://code.visualstudio.com/docs/python/linting) would pick up, even without executing – Cory Kramer Jun 22 '23 at 16:52
  • As stated by @jasonharper, the file is tokenized or compiled prior to execution and errors in Python syntax are caught during this process; however, that does not mean that errors such as referencing an undefined variable will be caught before the referencing line is executed. – Pragmatic_Lee Jun 22 '23 at 17:05

1 Answers1

4

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".

jsbueno
  • 99,910
  • 10
  • 151
  • 209