-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_Static variable creation.py
More file actions
190 lines (125 loc) · 3.43 KB
/
05_Static variable creation.py
File metadata and controls
190 lines (125 loc) · 3.43 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Static variable creation in various places.
# 1) inside a class directly :
# a)
class Student :
college = "DCE" #static variable / class level variable
s1 = Student()
print(s1.__dict__)
print(Student.__dict__)
# b) inside class but outside of constructor and method.
class Student() :
college = "DCE" #static variable / class level variable
branch = "Computer Science and Engineering"
def __init__(self, name, age, roll) :
self.name = name
self. age = age
self.roll = roll
def printDetails(self) :
print(f'Name is {self.name}')
print(f'Age is {self.age}')
print(f'Roll is {self.roll}')
print('-'*20)
s1 = Student('Priyanka', 23, 1024)
s1.printDetails()
s2 = Student('Zini', 24, 1025)
s2.printDetails()
s3 = Student('Jack', 25, 1026)
s3.printDetails()
print(s1.__dict__)
print(s2.__dict__)
print(s3.__dict__)
print(Student.__dict__)
# 2) Outside of the class(by using class name) :
# a)
class Student :
pass #empty class
s1 = Student()
Student.college = "DTU" #static variable / class level variable
Student.branch = "CSE" #staic variable
print(s1.__dict__)
print(Student.__dict__)
# b)
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.age = age
s1 = Student('Priyanka', 24, 1024)
s2 = Student('Rahul', 26, 1025)
s1 = Student('Anjali', 23, 1026)
Student.college = "JNU"
Student.branch = "MCA"
print(s1.__dict__)
print(s2.__dict__)
print(s3.__dict__)
print(Student.__dict__)
print(Student.__dict__)
# 3) inside the constructor(using class name) :
# a)
class Student :
def __init__(self) :
Student.college = "BHU" #static variable
s1 = Student()
s2 = Student()
print(s1.__dict__)
print(s2.__dict__)
print(Student.__dict__)
# b)
class Student :
def __init__(self, name) :
Student.college = "BHU" #static variable
self.name = name
s1 = Student('Priyanka')
s2 = Student('Archana')
print(s1.__dict__)
print(s2.__dict__)
print(Student.__dict__)
# 4) inside instance method(using class method) :
# a)
class Student :
def create(self) :
Student.college = "DCE" #staic variable
s1 = Student()
s1.create()
print(s1.__dict__)
print(Student.__dict__)
# b)
class Student :
def create(self, name) :
Student.college = "DCE" #staic variable
self.name = name
s1 = Student()
s1.create('Rahul')
print(s1.__dict__)
print(Student.__dict__)
# 5) inside class method(using class name or cls variable) :
# a)
class Student :
@classmethod
def cm(cls) :
Student.college = "BHU" #static variable
s1 = Student()
Student.cm() #call to class method by using class name
print(s1.__dict__)
print(Student.__dict__)
# b) using cls variable :
class Student :
@classmethod
def cm(cls) :
cls.college = "DTU" #static variable
print(id(cls))
s1 = Student()
Student.cm() #call to class method by using class name
print(s1.__dict__)
print(Student.__dict__)
# cls and Student both are pointing to same object
print(id(Student))
# 6) inside static method(using class name) :
class Student :
@staticmethod
def sm() :
Student.college = "DCE"
s1 = Student()
s1.sm()
print(s1.__dict__)
print(Student.__dict__)