-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_Static variable accessing.py
More file actions
67 lines (43 loc) · 1.09 KB
/
06_Static variable accessing.py
File metadata and controls
67 lines (43 loc) · 1.09 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
# Static variable accessing in different places :
# 1) outside of the class :
class Student :
college = "DCE" #static variable
print(Student.college)
# 2) inside constructor :
class Student :
college = "DTU"
def __init__(self) :
print(Student.college)
s1 = Student()
# 3) inside instance method :
class Student :
college = "BHU"
def display(self) :
print(Student.college)
s1 = Student()
s1.display()
# 4) inside class method(using class name) :
# a)
class Student :
college = "CEB"
@classmethod
def cm(cls) :
print(Student.college) #accessing static variable using class name
Student.cm()
# b) using cls variable :
class Student :
college = "XYZ"
@classmethod
def cm(cls) :
print(cls.college) #accessing static variable using class name
Student.cm()
# 5) inside static method :
class Student :
college = "MNO"
@staticmethod
def sm() :
print(Student.college)
s1 = Student()
s1.sm()
print(Student.__dict__)
print(s1.__dict__)