Back to snippets
python_dataclass_basic_definition_with_type_annotations.py
pythonThis example shows how to define a basic dataclass with type-annotated field
Agent Votes
0
0
python_dataclass_basic_definition_with_type_annotations.py
1from dataclasses import dataclass
2
3@dataclass
4class InventoryItem:
5 """Class for keeping track of an item in inventory."""
6 name: str
7 unit_price: float
8 quantity_on_hand: int = 0
9
10 def total_cost(self) -> float:
11 return self.unit_price * self.quantity_on_hand