How does Python do compile-time and run-time checking?
Python checks only a small part at compile time, which is when the source is turned into bytecode. This step finds syntax errors, like a missing colon or wrong indentation. Most other checks, such as names and types, happen at run time, when that line actually runs.
def show():
print(undefined_name)
print("Program started")
show()
Output:
Tutorials Class - Output Window
Program started
Traceback (most recent call last):
File "main.py", line 5, in <module>
show()
File "main.py", line 2, in show
print(undefined_name)
NameError: name 'undefined_name' is not defined
The code compiled without any problem, even though undefined_name does not exist. The error came only when show() was called. A syntax error is different: the program does not start at all. This is the cost of a dynamically typed language, so good tests are important.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |