-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass_static_methods.py
More file actions
120 lines (64 loc) · 1.94 KB
/
class_static_methods.py
File metadata and controls
120 lines (64 loc) · 1.94 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class Calculator:
a = 10
b = 4
def add(self):
print(self)
print(Calculator)
return self.a + self.b
def sub(self):
return self.a - self.b
def multiply(self):
return self.a * self.b
def division(self):
return self.a / self.b
# calci1 = Calculator()
# calling add() using object
# print(calci1.add())
# calling add() using class
# print(Calculator.add(calci1))
##############################################################################
# class method
class Employee:
name = "John"
id = 10
@classmethod
def display(cls):
print(cls.name, cls.id)
def spam(self):
print("in spam method")
e = Employee()
# e.display()
# Employee.display()
# Employee.spam(Employee())
#########################################################################
# modifying class variables using object address
class Employee:
company_name = "TYSS"
@classmethod
def change_of_company(cls, new_company):
cls.company_name = new_company
hr = Employee()
emp = Employee()
# print("before modification")
# print(emp.company_name) # TYSS
# print(hr.company_name) # TYSS
#
# emp.change_of_company("Infosys")
# print("\nafter modification")
# print(emp.company_name) # Infosys
# print(hr.company_name) # Infosys
##############################################################################
# creating alternate constructor
class Sample:
def __init__(self, date, month, year):
self.date = date
self.month = month
self.year = year
@classmethod
def split_date(cls, date_string):
date, month, year = date_string.split("-")
object_ = cls(date, month, year)
return object_
s1 = Sample.split_date("18-4-2020")
print(s1.date)
print(s1.month)