-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class and Instance varibles.py
50 lines (44 loc) · 1.48 KB
/
Class and Instance varibles.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
'''
Instance variables ---> These are the variables that can only be accessed by class object and class method
eg. to modify variable value, read
obj.varName
Class Variables: are also called as static variables
These variables are maintained and used among all the object of a class
To access these variables we need to refer them by using class Name
eg.
ClassName.ClassVariable
class DemoStaticVar:
collegeName = "ACS" # Static or class variable---> it is used commonly by all the objects
def __init__(self):
self.RN = None # instance variable
self.name = None # instance variable
def getData(self,rn,nm):
self.RN= rn
self.name = nm
def display(self):
print(f'Roll no = {self.RN}\tStudent Name = {self.name}')
print('College Name = ',DemoStaticVar.collegeName)
obj = DemoStaticVar()
obj.display()
obj.getData(10,'John')
obj.display()
obj2 = DemoStaticVar()
obj3 = DemoStaticVar()
obj4 = DemoStaticVar()
obj2.display()
obj3.display()
obj4.display()
##############################################################################################################
'''
class DemoObjCount:
count = 0
def __init__(self):
DemoObjCount.count= DemoObjCount.count+1
print(f'Object number {DemoObjCount.count} is created')
o1 = DemoObjCount()
o2 = DemoObjCount()
e = DemoObjCount()
h = DemoObjCount()
g = DemoObjCount()
ob= DemoObjCount()
print('Total Objects Created = ',DemoObjCount.count)