-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_instance variable.py
More file actions
185 lines (142 loc) · 4.47 KB
/
03_instance variable.py
File metadata and controls
185 lines (142 loc) · 4.47 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# creation of Instance variable :
# 1) creation of instance variable inside a constructor :
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
s1 = Student('priyanka', 22, 1011)
print(s1.__dict__)
# 2) creation of instance variable inside a instance method.
class Student() :
def create(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
s2 = Student()
print(s2.__dict__)
s2.create('rahul', 24, 1024)
print(s2.__dict__)
# 3) creation of instance variable outside of the class.
class Student :
def __init__(self, name) :
self.name = name
s3 = Student('zini')
print(s3.__dict__)
s3.age = 25 # creation of instance variable outside of class using referance variable
print(s3.__dict__)
s3.roll = 1025
print(s3.__dict__)
# 4) Programms on instance variable :
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
def printDetails(self) :
print(f'name is {self.name}')
print(f'age is {self.age}')
print(f'roll is {self.roll}')
s1 = Student('priyanka', 23, 1024)
s1.address = 'BBSR'
s2 = Student('rahul', 24, 1025)
s2.adrees = 'CTC'
s3 = Student('zini', 25, 1026)
s3.address = 'Nayagarh'
s4 = Student('jack', 24, 1027)
s4.address = 'Sonpur'
print(s1.__dict__)
print(s2.__dict__)
print(s3.__dict__)
print(s4.__dict__)
# Accessing and Deleting of instance variable.
# 1) accessing instance variable :
# access within the class :
# a)
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
#accessing instance variable within the class inside a instance method.
def printDetails(self) :
print(f'name is {self.name}')
print(f'age is {self.age}')
print(f'roll is {self.roll}')
s1 = Student('priyanka', 24, 1024)
s1.printDetails()
# b)
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
#accessing instance variable inside the class present inside the constructor.
print(f'name is {self.name}')
print(f'age is {self.age}')
print(f'roll is {self.roll}')
s1 = Student('priyanka', 24, 1026)
# access inside variable outside of the class :
# a)
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
s1 = Student('Zini', 23, 1023)
#accesing instance variable outside of the class using object referance
print(f'name is {s1.name}')
print(f'age is {s1.age}')
print(f'roll is {s1.roll}')
# b)
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
s1 = Student('Zini', 23, 1023)
#accesing instance variable outside of the class using object referance
print(f'name is {s1.name}')
print(f'age is {s1.age}')
print(f'roll is {s1.roll}')
s2 = Student('Jack', 25, 1025)
print(f'name is {s2.name}')
print(f'age is {s2.age}')
print(f'roll is {s2.roll}')
# Deletion of instance variable.
# delete from inside the class :
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
#delete instance variable inside a class present inside instance method.
def deleteData(self) :
del self.name
del self.roll
s1 = Student('Anjali', 25, 1026)
print(s1.__dict__)
s1.deleteData()
print(s1.__dict__)
# delete instance variable inside the class inside the constructor.
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
print(self.__dict__) #it will give details of instance variable
#delete instance variable inside class inside a constructor
del self.name
s1 =Student('Rashmi', 22, 1027)
print(s1.__dict__)
# Deletion of instance variable outside of class
class Student :
def __init__(self, name, age, roll) :
self.name = name
self.age = age
self.roll = roll
s1 = Student('Archana', 25, 1024)
print(s1.__dict__)
#delete instance variable outside of the class using referance variable.
del s1.age, s1.roll
print(s1.__dict__)