Junior — Middle
What is meant by typing in Python and how is it implemented?
sobes.tech AI
Answer from AI
Type hinting in Python is a system that defines which data types can be used and how they interact.
Python uses dynamic and strong typing:
- Dynamic — the type of a variable is determined at runtime, not beforehand.
- Strong — operations between incompatible types cause errors, for example, you cannot add a string and a number without explicit conversion.
Implementation of typing in Python:
- Variables are not bound to a type, but to an object that has a type.
- Object types are determined at runtime.
- There is support for type annotations (type hints) for static analysis, but they do not affect execution.
Example of dynamic typing:
x = 10 # x references an int
x = "text" # now x references a str
Example of strong typing:
print(5 + "5") # Error TypeError
Type annotations:
def greet(name: str) -> str:
return "Hello, " + name