employee = {
"name": "Alice",
"position": "Software Engineer",
"skills": ["Python", "JavaScript", "SQL"],
"contact": {
"email": "alice@example.com",
"phone": "555-1234"
}
}
You will use dicts even if you don’t want to:
This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements.
how to deal with this:
collections.defaultdictcollections.userdictIt’s definition: mutable. It can be hard to trace back.
type.MappingProxyTypedict to dataclass
from dataclasses import dataclass
import requests
@dataclass
class Pokemon:
weight: int
name: str
order: int
j = requests.get("https://pokeapi.co/api/v2/pokemon/bulbasaur").json()
bulba = Pokemon(**{k: j[k] for k in ["weight", "name", "order"]})
#python