-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_and_static_methods.py
More file actions
64 lines (43 loc) · 1.71 KB
/
class_and_static_methods.py
File metadata and controls
64 lines (43 loc) · 1.71 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Employee:
raise_amt = 1.04 # class variable
emp_no =0
def __init__(self, first,last,pay):
self.first = first
self.last = last
self.pay = pay
self.email = first + '.' + last + '@company.com'
Employee.emp_no += 1
def fullName(self):
return f'{self.first} {self.last}'
def calcRaise(self):
self.pay= self.pay * self.raise_amt
@classmethod # decorator. Changes fucnationality of the program. Now one method that follows is a class method
def set_raise_amt(cls, amt): #cls is the class variable. The class is automatically passed as first variable
cls.raise_amt = amt;
#using class method as a contructor. starts with 'from-...' by convention
@classmethod
def from_string(cls, emp_str):
first,last,pay = emp_str.split('-')
return cls(first,last,pay) # returns the object with the split variables parsing the string
@staticmethod #follwing method is a static method
def isWorkday(day): # doesn't pass class or instance as first argument
if day.weekday == 5 or day.weekday==6:
return False
return True
emp_1 = Employee ('Tanzil' , 'Rahman' , 69000)
emp_2 = Employee ('Omair' , 'Sux' , 4200)
print(Employee.raise_amt)
print(emp_1.raise_amt)
print(emp_2.raise_amt)
Employee.set_raise_amt(1.10) #since its a class method, it alters the raise_amt for the whole class
#can also run class methods from instances with the same effect
print(Employee.raise_amt)
print(emp_2.raise_amt)
print(emp_1.raise_amt)
emp_str_1 = 'John-Doe-690'
emp_str_2 = 'Jiju-Sawa-34'
cls_const_emp = Employee.from_string(emp_str_1) # calls from_string which returns the class object
print(cls_const_emp.email)
import datetime
my_date = datetime.date(2016 , 7 , 10)
print(Employee.isWorkday(my_date)) # calling a static