Oops in Python
Oops in Python
OOPS
class :
object :
In [15]: class comp:
def __init__(self):
self.var1=1
self.var2=3
def fun_sum(self):
print(self.var1+self.var2)
c1=comp()
comp.fun_sum(c1)
In [2]: type(c1)
__main__.comp
Out[2]:
class function :
In [22]: class temp:
def __init__(self,a):
self.x=a
def fun(self):
print("class function working")
t1=temp(47)
t1.fun()
temp.fun(t1)
print(t1.x)
constructor :
In [13]: class temp1:
def __init__(self):
print("constructor is working")
def fun(self,x):
print(x)
def __init__(self,name,no):
print(f"name :{name} with roll no:{no}")
t1=temp1('manoj',47)
t1.fun(5)
t2=temp1()
t2.fun(47)
t1=temp2()
t2=temp2()
temp2.wheels=3
print(t1.wheels)
print(t2.wheels)
t1.wheels=2
print(t1.wheels)
print(t2.wheels)
3
3
2
3
t1=st()
t1.sett('x',5)
t1.get('x')
t1.ret()
t1.info() # here it is called with object because it is present in a class
5
47
its just a class method of static type
def show(self):
print(f"roll no: {self.id} name : {self.name}")
self.l.show()
class info():
def __init__(self):
self.dept="IT"
self.gpa="B.Tech 3rd year "
def get_data(self,dept,gpa):
self.gpa=gpa
self.dept=dept
def show(self):
print(f"department of {self.dept} in {self.gpa}")
t1=student("Manoj",47)
t2=student("Vyshnavi",41)
t1.show()
t2.show()
#"""t1.info.get_data("IT",7.02)
#t2.info.get_data("IT",7.5)
#t1.info.show()
#t2.info.show()"""
Inheritance
single inheritance :
In [17]: class a:
def fun1(self):
print("fun1 is working")
def fun2(self):
print("fun2 is working")
class c(a,b):
def fun5(self):
print("fun5 if working")
t1=a()
t=c()
class a is intialised
class b is initialised
multi-level inheritance :
In [18]: class a:
def fun1(self):
print("fun1 is working")
def fun2(self):
print("fun2 is working")
class b(a):
def fun3(self):
print("fun3 is working")
def fun4(self):
print("fun4 is working")
class c(b):
def fun5(self):
print("fun5 if working")
t1=a()
t2=b()
t=c()
multiple inheritance :
In [26]: class a:
def fun1(self):
print("fun1 is working")
def fun2(self):
print("fun2 is working")
class b:
def fun3(self):
print("fun3 is working")
def fun4(self):
print("fun4 is working")
class c(a,b):
def fun5(self):
print("fun5 if working")
t1=a()
t2=b()
t=c()
super() function:
this function is used to access the functions and construstors and variable of
the parent class
t=b()
t.fun()
print(t.val)
class a is intialised
class b is initialised
function of class a
10
polymorphisim
Duck typing:
it is based on the charcteristic of a class of function
In [ ]:
operator overloading :
there are specific function in python that works as operators:
In [19]: class s:
def __init__(self,v1,v2):
self.v1=v1
self.v2=v2
def __add__(self,other):
v1=self.v1+other.v1
v2=self.v2+other.v2
obj=s(v1,v2)
return obj
localhost:8888/nbconvert/html/DS through Python/My learnings.ipynb?download=false 6/11
5/27/23, 10:30 AM My learnings
def __sub__(self,other):
v1=self.v1-other.v1
v2=self.v2-other.v2
obj=s(v1,v2)
return obj
def __mul__(self,other):
v1=self.v1*other.v1
v2=self.v2*other.v2
obj=s(v1,v2)
return obj
t1=s(1,2)
t2=s(2,1)
t3=t1+t2
print(t3.v1,t3.v2)
t3=t1-t2
print(t3.v1,t3.v2)
t3=t1*t2
print(t3.v1,t3.v2)
3 3
-1 1
2 2
Method Overloading
In [11]: class a:
def add(self,c=None,d=None,e=None):
s=0
if c!=None and d!=None and e!=None:
s=c+d+e
elif c!=None and d!=None:
s=c+d
else:
s=c
return s
t=a()
print(t.add(1,2,3))
print(t.add(1,2))
print(t.add(1))
6
3
1
method overriding
In [18]: class a:
def show(self):
print("in class a")
class b(a):
def show(self):
print("in class b")
super().show()
t=b()
t.show()
a.show(a)
t.super().show()
in class b
in class a
in class a
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [18], in <cell line: 12>()
10 t.show()
11 a.show(a)
---> 12 t.super().show()
while calling an object it predinedly calls the str() method which gives the
address of the object as shown below
In [10]: #sai
class b:
def __init__(self,c,d):
self.v1=c
self.v2=d
def show(self):
print(self.v1,self.v2)
class a:
def __init__(self,c,d):
self.v1=c
self.v2=d
def show(self):
print(self.v1,self.v2)
def __str__(self):
return '{} {}'.format(self.v1,self.v2)
t=a(1,2)
t.show()
print(t)
t=b(1,2)
t.show()
print(t)
1 2
1 2
with out the overridding of __str__() method
1 2
<__main__.b object at 0x00000222A1D29AC0>
In [ ]: