best-practices
Type Stability Best Practices
Keep variable types consistent for maximum optimisation.
Published May 30, 2026
The Rule
Once a variable has a type, keep it that type for the entire function.
Good Example
def sum_ints(data):
total: int = 0
for x in data:
total += x
return totalBad Example
def mixed(data):
total = 0
for x in data:
if x > 10:
total = str(total) # type change!
total += x