-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstance_variable | xpython.py
More file actions
132 lines (83 loc) · 3.2 KB
/
instance_variable | xpython.py
File metadata and controls
132 lines (83 loc) · 3.2 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
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> INSTANCE VARIABLES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1.The variables which are declared inside the method using self word is called instance variable.
2. The variables which holds the unique values for every object.
3. Instance variables will get memory for every object.
4. To call instance variables we use object variable.
5. Instance variables will get memory after object creation.
Syntax:-
class_name()
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ OBJECT $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ Object is an instance of a class. Instance means allocating sufficient memory for a instance variable of a class. $
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
___________________________________________ Program on instance variables _________________________________________________
class Student: # class
def assignment(self): # instance method
self.id = 1010 # instance variable
self.name = 'jin' # instance variable
self.contact = 10010101000010 # instance variable
def display(self):
print(self.id)
print(self.name)
print(self.contact)
s = Student()
s.assignment()
s.display()
****************************************** WE CAN CREATE 'n' NUMBERS OF OBJECT************************************************
class Employee:
def assign(self, id, na, sal = 0.0):
self.idno = id
self.name = na
self.salary = sal
def display(self):
print(self.idno)
print(self.name)
print(self.salary)
e1 = Employee() # first object
e1.assign(100, 'jb')
e1.display()
print("---------")
e2 = Employee()
e2.assign(1010, 'Mr.chao', 250000.00)
e2.display()
print("---------")
e1.salary = 180000.00
e1.name = 'kim zong yang'
e1.display()
print("---------")
e2.salary = 3750000.00
e2.display()
##############################################################################################################################
x:- INSTANCE VARIABLES
class Employee:
def assign(self, idno, name, salary):
self.idno = idno
self.name = name
self.salary = salary
def disp(self):
print(self.idno)
print(self.name)
print(self.salary)
def show():
e = Employee() # local variable 'e'
e.assign(1010, 'jb', 2564000.00)
e.disp()
print("outside class")
show()
################################### Example on declaring object as local and global #############################################
class Em:
def name_(self, idno, name, salary):
self.idno = idno
self.name = name
self.salary = salary
def dp(self):
print(self.idno)
print(self.name)
print(self.salary)
f_o = Em() # global var
def sh():
s_o = Em() # local var
s_o.name_(100, 'me', 98032000.00)
s_o.dp()
sh() # function call
f_o.name_(1001010, 'class Em', 5404000.00)
f_o.dp()