How do you communicate via code what argument types and what return value type a function has?

Experience Level: Junior
Tags: Python

Answer

Always use type hints.

Example:

def get_messages(template: str, number: int) -> list[str]:
    message1 = "Message template: " + template
    message2 = "Number: " + str(number)

    return [message1, message2]


print(get_messages("Hello", 1))

# This will print ['Message template: Hello', 'Number: 1']

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself