The aim of this page? is to explain how to use type hints in Python, specifically for functions that return a list of dictionaries.
I am slowly going through David Baezley's Advanced Python mastery and - based on How to Code's systematic approach to program design - I am annotating functions with input and output types as that definition determines the shape of the function.
from typing import List, Dict import csv def read_rides(filename: str) -> List[Dict]: rides = [] with open(filename, "r") as file: rows = csv.reader(file) headers = [row.strip() for row in next(rows)] print(f"ROW headers: {headers}") for row in rows: ride = {} for column_number, column_name in enumerate(headers): ride[column_name] = row[column_number].strip() rides.append(ride) return rides
https://peps.python.org/pep-0484/#the-typing-module
https://github.com/dabeaz-course/python-mastery/blob/main/Exercises/ex2_2.md
https://htdp.org/2022-2-9/Book/part_one.html#(part._sec~3adesign-func)
The above is the detailed content of How to Use Typing Module to Annotate Function Definition with Input and Output Types in Python. For more information, please follow other related articles on the PHP Chinese website!