-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-Modules.py
More file actions
45 lines (37 loc) · 1.75 KB
/
16-Modules.py
File metadata and controls
45 lines (37 loc) · 1.75 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
# using 'import' keyword
# Alias name for an imported module
# Import part of a module - using 'from' keyword
from builtins import staticmethod
import item
class Order :
companyName = 'Walmart Inc.' # class level variable -- this variable can
# be accessed by Class method, but not static method.
def __init__(self, orderDate, itemList, totalPrice) :
self.orderDate=orderDate
self.itemList=itemList
self.totalPrice=totalPrice
def calculateOrderValue(self,itemList) :
totalPrice=0
for item in itemList :
totalPrice+=item.price
return totalPrice
# class method - similar to static method in C++, Java
def changeComanyName(cls,companyNm) :
Order.companyName = companyNm
print('After change, Company name : ', Order.companyName, ', Name : ')
# static method - can not access both class & instance variables.
@staticmethod
def checkOrdersCount_ForMonth() :
# can not access both class & instance variables.
# print('Company name : ', companyName)
pass
def __str__(self) : # similar to toString() , in Java language
orderDetails='OrderDate : ' + self.orderDate + '\n '
for item in self.itemList :
orderDetails +='\n Item name : ' + item.name
return orderDetails
item_1 = item.items('Lenova Laptop', 1500)
item_2 = item.items('Dell Laptop', 2200)
order1 = Order('17-Jul-2024',[item_1,item_2] ,0 )
print('Order_1 instance method : ', order1.calculateOrderValue([item_1,item_2]))
print('Order Class method : ', Order.changeComanyName(Order,'Amazon Incorporation'), 'Static method : ' , Order.checkOrdersCount_ForMonth())