Junior — Middle
What is static and dynamic typing in Python and how are they implemented in the language?
sobes.tech AI
Answer from AI
Python uses dynamic typing: the variable's type is determined at runtime, not in advance. This means that a variable can be assigned a value of any type, and the variable's type can change during the program's execution.
Static typing is when the variable's type is determined at compile time and does not change. In Python, static typing is not implemented by default, but with the advent of type annotations (type hints) and static analysis tools (e.g., mypy), it is possible to add type information for code checking without executing.
Example of dynamic typing in Python:
x = 10 # x is an integer
x = 'text' # now x is a string
Example of annotations for static checking:
def greet(name: str) -> str:
return 'Hello, ' + name
Here, types are specified, but Python does not check them at runtime; external tools do.