-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_Static variable modification.py
More file actions
115 lines (74 loc) · 1.95 KB
/
07_Static variable modification.py
File metadata and controls
115 lines (74 loc) · 1.95 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
# Modify Static variable value :
# 1) outside of the class :
class Student :
college = "CEB" #static variable
print(Student.college) #before modification
Student.college = "DCE"
print(Student.college)
# 2) inside constructor :
class Student :
college = "DCE"
def __init__(self) :
Student.college = "DTU"
print(Student.college)
s1 = Student() #after modification
print(Student.college)
# 3) inside instance method :
class Student :
college = "COE"
def modify(self) :
Student.college = "BHU"
print(Student.college)
s1 = Student()
s1.modify()
print(Student.college)
# 4) inside class method(using class name) :
# a)
class Student :
college = "DTU"
@classmethod
def cm(cls) :
Student.college = "CEB" #modify
print(Student.college)
s1 = Student()
s1.cm()
print(Student.college)
# b) using cls variable :
class Student :
college = "DTU"
@classmethod
def cm(cls) :
cls.college = "STV" #modify
print(Student.college)
s1 = Student()
s1.cm()
print(Student.college)
# 5) inside static method :
class Student :
college = "MNT"
@staticmethod
def sm() :
Student.college = "OPT"
print(Student.college)
s1 = Student()
s1.sm()
print(Student.college)
# Modifying static variable values using self / object referance :
# a) modify using self(unexpected result) :
class Student :
college = "XYZ"
def __init__(self) :
self.college = "MNT" # it will create instance variable instead of modifying static variables
print(Student.college)
s1 = Student()
print(Student.college)
print(Student.__dict__)
print(s1.__dict__)
# b) using object referance variable :
class Student :
college = "XYZ"
s1 = Student()
print(Student.college)
s1.college = "COEB" # it will create instance variable for s1 object
print(Student.college)
print(s1.__dict__)