I want to understand when and how, the various compile time, and run time checks happen in Python. Thank you!
python is an interpreter so it does caching of the AST you can call it compiling but it's not as intense.
usually you have 4 to 5 steps in programming languages (the optimizer/interpreter/compiler part varies)
and the interpreter does the type inference in your case. Because think of it : type inference on dynamic data in a weak typed language* has to be done as the program gets executed otherwise how will it know what it is ?
The "compiled" data in python (I ignore pypy and such projects) is the optimized AST in Bytecode so the interpreter does not have to parse all files again but it's a cache not a compiled file :)
Edit: Thx Sai Kishore Komanduri for pointing out that's not a weak typed but a dynamic typed language. Didn't think it through :)
In addition to what j and Sai wrote, it bears mentioning that python supports type annotations.
They make no difference at execution time and the code will work the same with or without annotations but, if used, an extra step to development can be added to run a type checking tool for catching type errors within the application.
Runtime and compile time are programming terms that refer to different stages of software program development. Compile-time is the instance where the code you entered is converted to executable while Run-time is the instance where the executable is running. The terms "runtime" and "compile time" are often used by programmers to refer to different types of errors too.
Compile-time checking occurs during the compile time. Compile time errors are error occurred due to typing mistake, if we do not follow the proper syntax and semantics of any programming language then compile time errors are thrown by the compiler. They wont let your program to execute a single line until you remove all the syntax errors or until you debug the compile time errors. The following are usual compile time errors:
Syntax errors
Typechecking errors
Compiler crashes (Rarely)
More on.... Runtime vs Compile time
Sai Kishore Komanduri
Engineering an eGovernance Product | Hashnode Alumnus | I love pixel art
Python does type inference during execution time. As j has pointed out Python is a dynamically typed language; meaning you don't have to specify the type of a variable during its declaration.
Python has typed objects, though; but even after the fact, Python does what is called duck typing. What this means is, an object doesn't have to be of a certain type — during compile time — when one is trying to call a method on that object. If any type errors happen, they are reflected during run time.
In the above regard, contrary to what j has pointed out; Python is not a weakly-typed language; in fact it is a strongly typed language; meaning that you cannot do operations on two objects that are of different types (in contrast to what you can do in JavaScript,
"x" + 5will fail in Python)When someone mentions, a strongly-typed language; more often than not, that language also turns out to be a statically typed language. Python would be an odd one out here — being a dynamically typed language, it is also a strongly typed one.