What if you need to work with Python versions before 3.5 and you still wanna use types.
Or maybe you don’t want to stub your entire library of code, you don’t wanna hint everything, just a file or two at a time. This is where a very handy feature known as stub files comes in.
calculator.pyi
from numbers import Real
def add(num1: Real, num2: Real) -> Real
def sub(num1: Real, num2: Real) -> Real
def multiply(num1: Real, num2: Real) -> Real
def divide(num1: Real, num2: Real) -> Real
calculator .py
def add(num1, num2):
return num1 + num2
def sub(num1, num2):
return num1 - num2
def multiply(num1, num2):
return num1 * num2
def divide(num1, num2):
return num1 / num2
print(sub(9, 3))
print(add(5, 8.2))
print(multiply('Hello', 5))
Now if you’re using Python 2.7 or 2.3 or whatever, you’ll never know that these type hints are there, that they’re available.