Aan de slagGa gratis aan de slag

Using the Typing library

The typing library helps when adding type hints to objects such as dictionaries and lists, as well as the elements in those objects. In this exercise, you'll practice exactly that. Go get 'em!

Deze oefening maakt deel uit van de cursus

Intermediate Object-Oriented Programming in Python

Cursus bekijken

Oefeninstructies

  • From the typing library, import Dict and List.
  • Using a class imported from typing, add type hints to the roster dictionary to include details about the types of keys and values.
  • Update the creation of the agents list to add type hints for both the object itself, as well as the type of the elements it stores.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Import Dict and List from typing
from ____ import ____, ____

# Type hint the roster of codenames and number of missions
roster: ____[____, ____] = {
  "Chuck": 37,
  "Devin": 2,
  "Steven": 4
}

# Unpack the values and add type hints for the new list
agents: ____[____] = [
  f"Agent {agent}, {missions} missions" \
  for agent, missions in roster.items()
]
Code bewerken en uitvoeren