B16 - data classes

sources: python docs, ArjanCodes

from dataclasses import dataclass

@dataclass
class InventoryItem:
    """class for keeping track of an item in inventory"""
    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand
def __init__(self, name: str, unit_price: float, quantity_on_hand: int = 0):
    self.name = name
    self.unit_price = unit_price
    self.quantity_on_hand = quantity_on_hand
@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False,
           match_args=True, kw_only=False, slots=False, weakref_slot=False)
from dataclasses import dataclass, field

@dataclass(order =True)
class Student:
	sort_index : int = field(init=False)
	name: str
	grade: int
	roll_num: int
	
	def __post_init__(self):
		self.sort_index = self.age