CAP/TP02/python/typecheck.py

16 lines
385 B
Python
Raw Normal View History

2024-09-23 11:04:28 +02:00
# Typing annotations for variables:
# name: type
int_variable: int
float_variable: float
2024-09-30 14:32:21 +02:00
int_variable = 4 # Static typing error, but no runtime error
2024-09-23 11:04:28 +02:00
float_variable = 42.0 # OK
float_variable = int_variable # OK
# Typing annotations for functions (-> means "returns")
def int_to_string(i: int) -> str:
return str(i)
2024-09-30 14:32:21 +02:00
print(int_to_string(42)) # Both static and runtime error