Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Function annotations in Python are a way to provide hints or metadata to the users of your functions about the expected types of the function's arguments and return values. They were introduced in Python 3.0 and are not mandatory, but they can be helpful for understanding the intended usage of a function.
Here's a tutorial on how to use function annotations in Python 3:
The basic syntax for annotating a function's arguments and return value is as follows:
def function_name(arg1: annotation1, arg2: annotation2) -> return_annotation: # function body
The colon :
followed by the annotation is used to annotate the function arguments, and the ->
symbol followed by the annotation is used to annotate the return value of the function.
You can use annotations to provide hints about the expected types of a function's arguments:
def greet(name: str, age: int) -> None: print(f"Hello, my name is {name} and I'm {age} years old.")
In this example, the greet
function has two arguments: name
and age
. The name
argument is annotated as a str
(string) type, and the age
argument is annotated as an int
(integer) type.
You can use annotations to provide hints about the expected return value of a function:
def add_numbers(a: int, b: int) -> int: return a + b
In this example, the add_numbers
function is annotated to return an int
(integer) value.
You can access a function's annotations through its __annotations__
attribute, which is a dictionary containing the argument names as keys and the annotations as values. The return value annotation is stored with the key 'return'
.
def example_function(arg: str) -> int: pass print(example_function.__annotations__)
This will output:
{'arg': <class 'str'>, 'return': <class 'int'>}
typing
module for complex typesFor more complex type hints, you can use the typing
module, which provides additional type hinting options, like List
, Dict
, Tuple
, Union
, and more.
Here's an example using the typing
module:
from typing import List, Tuple def sum_and_product(numbers: List[int]) -> Tuple[int, int]: _sum = sum(numbers) _product = 1 for number in numbers: _product *= number return _sum, _product
In this example, the sum_and_product
function takes a list of integers as an argument and returns a tuple containing the sum and the product of the numbers.
In summary, function annotations in Python 3 provide a way to add metadata or hints about the expected types of a function's arguments and return values. They are not mandatory and do not affect the runtime behavior of the function, but they can be useful for understanding the intended usage of a function and improving the readability of your code. Using the typing
module, you can create more complex and expressive type hints.
How to use function annotations for type hints in Python 3:
->
syntax.def add(x: int, y: int) -> int: return x + y
Providing type information with function annotations in Python 3:
def greet(name: str) -> str: return f"Hello, {name}!"
Using type hints for parameters and return values in Python 3 functions:
def divide(dividend: float, divisor: float) -> float: return dividend / divisor
Type hinting for optional and default parameters in Python 3:
def greet_person(name: str, greeting: str = "Hello") -> str: return f"{greeting}, {name}!"