-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreating_Class_and_Objects.py
145 lines (105 loc) · 3 KB
/
Creating_Class_and_Objects.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
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
#**CLASS & FUNCTIONS**
# Class means a blueprint of code which consists of a functions and methods.
#constructor -> objects
class www:
name="dinesh"
id=23
x=www() # a variable , assigned to a class -> object/instance
x
x.name , x.id #=> Object variable
dir(www)
dir(str)
a="asdf"
print(type(a))
www()
dir(www)
class emp:#2
def __init__(self,id,name,age): #3 self=e1,id=2333,name=gowri,age=32
self.ID=id #4 e1.ID = 2333
self.Name=name #5 e1.Name=gowri
self.Age=age #6 e1.Age=32
location="salem" #7 e1.location=salem
e1=emp(2333,"gowri",32) #1 creates obj e1 based on emp go to __init__ #8 sucess
e1
e1.ID,e1.Name,e1.location,e1.Age
class emp:
def __init__(self,id,name,age):
self.ID=id
self.Name=name
self.Age=age
def show(aaa):#2 aaa=e1
print(aaa.ID,aaa.Name,aaa.Age,aaa) #3 e1.ID,e1.Name,e1.Age
location="salem"
e1=emp(2333,"gowri",32)
e1
e1.show() #1 calls show() #4 sucess
e2=emp(1234,"adhi",12)
e2
e2.show()
e1.__init__(6789,"hameesh",22)
e1.show()
class emp:
def __init__(self,id,name,age):
self.ID=id
self.Name=name
self.Age=age
def show(aaa):#2 aaa=e1
print(aaa.ID,aaa.Name,aaa.Age,aaa)
def modify(self,new_name):#2 self=e1,new_name=vishnu
self.Name=new_name #3 e1.Name=vishnu
e1=emp(2333,"gowri",32)
e1.show()
e1.modify("vishnu") #1 call #4 sucess
e1.show()
class emp:
def __init__(self,id,name,age):
self.ID=id
self.Name=name
self.Age=age
def show(aaa):
print(aaa.ID,aaa.Name,aaa.Age,aaa)
def modify(self,new_name):
self.Name=new_name
class guvi(emp):
pass #PASS: is a placeholder, we use 'pass' when the function has not need to do anything.
#This is helpful when you are in the early stages of development and are still planning the program's structure.
e3=guvi(1222,"suresh",54)
e3.show()
class emp:
def __init__(self,id,name,age):
self.ID=id
self.Name=name
self.Age=age
def show(aaa):
print(aaa.ID,aaa.Name,aaa.Age,aaa)
def modify(self,new_name):
self.Name=new_name
class guvi(emp):
def show(self):
print("G"+str(self.ID),self.Name,self.Age)
e4=guvi(1222,"suresh",54)
e4.show()
class emp:
def __init__(self,id,name,age):
self.ID=id
self.Name=name
self.Age=age
def show(aaa):
print(aaa.ID,aaa.Name,aaa.Age,aaa)
def modify(self,new_name):
self.Name=new_name
class tcs(emp):
def addLocation(self,location):
self.loc=location
def show(self):
print("name",self.Name,"\nid","T"+str(self.ID),"\nage",self.Age,"\nlocation",self.loc)
class hcl(emp): # Here hcl class(child class) inherits the attributes and methods of the emp class(parent class).
def show(self):
print("name",self.Name,"\nid","H"+str(self.ID),"\nage",self.Age)
e5=emp(1111,"badri",34)
e5.show()
e6=tcs(1111,"badri",34)
e6.addLocation("chennai")
e6.show()
e7=hcl(1111,"badri",34)
e7.show()