-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop04.py
More file actions
30 lines (21 loc) · 1.06 KB
/
oop04.py
File metadata and controls
30 lines (21 loc) · 1.06 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
# creating a class
class Item:
# init is called automatically when an instance is created
# Using type hints for better code readability
# constructor
def __init__(self, name: str, price: float, quantity=0):
# Run validation to the received arguments
assert price >= 0, f"Price {price} is not greater than or equal to zero!"
assert quantity >=0, f"Quantity {quantity} is not greater than or equal to zero!"
# assigning to self object
self.name = name # instance attributes
self.price = price # instance attributes
self.quantity = quantity # instance attributes
def calculate_total_price(self):
return self.price * self.quantity
item1 = Item("phone", 100, 5) # creating an instance of the Item class
item2 = Item("Laptop", 1000, 6) # creating another instance of the Item class
# whether a laptop has a numpad or not (not for phone)
item2.has_numpad = False # adding attribute to only item2 instance
print(item1.calculate_total_price())
print(item2.calculate_total_price())