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']
Related Python job interview questions
What is a convention to name a protected method in Python?
Python JuniorWhat is a convention to name a private method in Python?
Python JuniorHow do you define a function in Python?
Python JuniorHow can you display all modules that are currently loaded in your Python program?
Python JuniorWhat happens if you import a same module in Python twice and the module contains code print("hello")?
Python Junior