5/27/23, 10:30 AM My learnings
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)
class function working
class function working
47
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)
localhost:8888/nbconvert/html/DS through Python/My learnings.ipynb?download=false 1/11
5/27/23, 10:30 AM My learnings
name :manoj with roll no:47
5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [13], in <cell line: 11>()
9 t1=temp1('manoj',47)
10 t1.fun(5)
---> 11 t2=temp1()
12 t2.fun(47)
TypeError: __init__() missing 2 required positional arguments: 'name' and 'no'
class variable manupulation
In [25]: class temp2():
wheels=4
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
static method in class
In [6]: class st():
y=47
def __init__(self):
self.x=0
def sett(self,s,val):
self.s=val
def get(self,s):
print(self.s)
#@classmethod
def ret(self):
print(self.y)
@staticmethod # the static method doesn't need a object to pass like self bu
def info():
print("its just a class method of static type")
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
class inside a class
localhost:8888/nbconvert/html/DS through Python/My learnings.ipynb?download=false 2/11
5/27/23, 10:30 AM My learnings
In [21]: # class inside a class
class student():
def __init__(self,name,id):
self.id=id
self.name=name
self.l=self.info()
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()"""
roll no: 47 name : Manoj
department of IT in B.Tech 3rd year
roll no: 41 name : Vyshnavi
department of IT in B.Tech 3rd year
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()
print("\n accessing with individual objects ")
t1.fun1()
t1.fun2()
localhost:8888/nbconvert/html/DS through Python/My learnings.ipynb?download=false 3/11
5/27/23, 10:30 AM My learnings
print("\n accessing with inherited class c")
t.fun1()
t.fun2()
t.fun5()
class a is intialised
class b is initialised
accessing with individual objects
fun1 is working
fun2 is working
accessing with inherited class c
fun1 is working
fun2 is working
fun5 if working
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()
print("\naccessing with individual objects")
t1.fun1()
t1.fun2()
t2.fun3()
t2.fun4()
print("\naccessing with inherited class c")
t.fun1()
t.fun2()
t.fun3()
t.fun4()
t.fun5()
localhost:8888/nbconvert/html/DS through Python/My learnings.ipynb?download=false 4/11
5/27/23, 10:30 AM My learnings
accessing with individual objects
fun1 is working
fun2 is working
fun3 is working
fun4 is working
accessing with inherited class c
fun1 is working
fun2 is working
fun3 is working
fun4 is working
fun5 if working
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()
print("\naccessing with individual objects")
t1.fun1()
t1.fun2()
t2.fun3()
t2.fun4()
print("\naccessing with inherited class c")
t.fun1()
t.fun2()
t.fun3()
t.fun4()
t.fun5()
accessing with individual objects
fun1 is working
fun2 is working
fun3 is working
fun4 is working
accessing with inherited class c
fun1 is working
fun2 is working
fun3 is working
fun4 is working
fun5 if working
localhost:8888/nbconvert/html/DS through Python/My learnings.ipynb?download=false 5/11
5/27/23, 10:30 AM My learnings
super() function:
this function is used to access the functions and construstors and variable of
the parent class
In [9]: # using super keyword
class a:
val=10
def __init__(self):
print("class a is intialised")
def fun(self):
print("function of class a")
class b(a):
def __init__(self):
super().__init__()
print("class b is initialised")
def fun1(self):
print("function of class b")
super().fun()
#print(super().val)
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:
add (var1,var2)" = var1 + var2
sub (var1,var2) = var1 - var2
mul (var1,var2) = var1 * var2
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
Method overloading and constructor is not possible in python
but to satisfy this we can use none variable with conditions
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")
localhost:8888/nbconvert/html/DS through Python/My learnings.ipynb?download=false 7/11
5/27/23, 10:30 AM My learnings
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()
AttributeError: 'b' object has no attribute 'super'
str() is a method which is predefined.
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)
print("with out the overridding of __str__() method")
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 [ ]:
localhost:8888/nbconvert/html/DS through Python/My learnings.ipynb?download=false 8/11