-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInstanceVariables.py
More file actions
35 lines (27 loc) · 1.08 KB
/
InstanceVariables.py
File metadata and controls
35 lines (27 loc) · 1.08 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
# Instance variables should be initialized before use
# There are three ways to initialize instance variables
# 1. Using Constructor 2. Using object 3. Using methods
class Student:
#Initializing using constructor
def __init__(self,name):
super().__init__()
self.name = name
#Initializing using methods
def defineAttributes(self,roll_no, age):
self.roll_no = roll_no
self.age = age
def display(self):
print('Name: ',self.name)
print('RollNo: ',self.roll_no)
print('Age: ',self.age)
print('Section: ',self.section)
s = Student('Student_A') #only name is initialized
#s.display() #will result into error
s.section='B' #section is initialized
s.defineAttributes(113,15) #roll no and age is initialized
s.display()
#Symbol table or special attribute __dict__ (holds dictionary of the instance variables)
print(s.__dict__) #Output: {'name': 'Student_A', 'section': 'B', 'roll_no': 113, 'age': 15}
#Deleting instance variables
del s.age
print(s.__dict__) #Output: {'name': 'Student_A', 'section': 'B', 'roll_no': 113}