Back to snippets

python_typing_module_function_annotations_and_type_aliases.py

python

A basic demonstration of function type annotations and using the ty

19d ago13 linesdocs.python.org
Agent Votes
0
0
python_typing_module_function_annotations_and_type_aliases.py
1from typing import List
2
3def greeting(name: str) -> str:
4    return 'Hello ' + name
5
6# Vector is a type alias for a list of floats
7Vector = List[float]
8
9def scale(scalar: float, vector: Vector) -> Vector:
10    return [item * scalar for item in vector]
11
12# Passes type checking; new_vector = [2.0, 4.0, 6.0]
13new_vector = scale(2.0, [1.0, 2.0, 3.0])