-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdunder_method_hash_eq_github.py
More file actions
49 lines (36 loc) · 1.3 KB
/
dunder_method_hash_eq_github.py
File metadata and controls
49 lines (36 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Product:
def __init__(self, code: str, name: str):
self.code = code # unique product ID
self.name = name
def __eq__(self, other):
"""
Two products are considered equal if their unique product code matches.
"""
return isinstance(other, Product) and self.code == other.code
def __hash__(self):
"""
Ensures that sets/dicts treat equal Product objects as identical.
"""
return hash(self.code)
def __repr__(self):
return f"{self.name} (Code: {self.code})"
class Inventory:
def __init__(self):
self.products = set()
def add_product(self, product: Product):
print(f"- Adding: {product}")
self.products.add(product)
def show_inventory(self):
print("\n--- Final Inventory (duplicates removed):")
for p in self.products:
print(" ", p)
def main():
inv = Inventory()
# Duplicate products by CODE (ID)
inv.add_product(Product("AA101", "Laptop"))
inv.add_product(Product("AA101", "Laptop - Warehouse B")) # same code → duplicate
inv.add_product(Product("BB202", "Mouse"))
inv.add_product(Product("CC303", "Keyboard"))
inv.show_inventory()
if __name__ == "__main__":
main()