-
Notifications
You must be signed in to change notification settings - Fork 2
/
class_data_variable.py
81 lines (64 loc) · 2.05 KB
/
class_data_variable.py
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
#!/usr/bin/python
# Date: 2018-09-03
#
# Description:
# Create, access and update class/static variable.
class CSStudent:
"""Demo to access and update class variable."""
stream = "CSE" # Class/static variable.
def __init__(self, roll):
self.roll = roll
a = CSStudent(101)
b = CSStudent(102)
print ("\nAssigned values...")
print (a.stream) # CSE
print (b.stream) # CSE
print (a.roll) # 101
print (b.roll) # 102
print ("\na.stream updated to IT...")
# If we try to change class variable using object, a new instance or
# non-static variable for that particular object is created and this variable
# shadows the class variables. This update will not be reflected to other
# objects.
a.stream = "IT"
print (a.stream) # IT
print (b.stream) # CSE
print (CSStudent.stream) # CSE
# When class variable is updated using class name, it will be reflected to all
# objects but not for those who has already shadowed or created a variable with
# same name as class variable like object 'a'.
print ("\nCSStudent.stream updated to ECE...")
CSStudent.stream = "ECE"
print (a.stream) # IT
print (b.stream) # ECE
print (CSStudent.stream) # ECE
print ('\n\nAnother example...')
class Counter:
"""
A demonstration of class attributes(Common across all class instances)
and data attributes(per instance).
"""
overall_count = 0 # Class attribute
def __init__(self):
self.my_total = 0 # Data attribute
def increment(self):
self.my_total += 1
Counter.overall_count += 1
# self.__class__.overall_count += 1 # This also works.
a = Counter()
b = Counter()
print (a.my_total) # 0
print (b.my_total) # 0
print (a.__class__.overall_count) # 0
print (b.__class__.overall_count) # 0
a.increment()
b.increment()
b.increment()
print (a.my_total) # 1
print (b.my_total) # 2
print (a.__class__.overall_count) # 3
print (b.__class__.overall_count) # 3
# Class attribute can also be accessed using object and class name.
print (a.overall_count) # 3
print (b.overall_count) # 3
print (Counter.overall_count) # 3