OOPS+Python Session 2
OOPS+Python Session 2
name="mayukh"
age=19
prof="data"
def info(self): #arguement of any function within a class is always the class
itself(self) with some other arguements(if necessary)
###########################################################constructor
class me:
breed="human" #class variable(but we can re assign its value during creating an
instance
no_person=0
def __init__(self,n,a): #constructor
print("CREATION")
self.name=n #instance variables
self.age=a
self.no_person+=1
##############################INHERITANCE##################################
class he(me):
gender="M"
def gd_info(self):
print("""THIS IS THE CHILD CLASS
GENDER IS """,self.gender)
z=he("mak",19)
z.info()
z.gd_info()
z.gender="F"
z.gd_info()
w=me("mayo",25)
w.info()
w.gd_info()#---->will give error as the inherited class cant call functions from
the child classes
####################################ACCESS MODIFIERS##################
# BY DEFAULT IN PYTHON THE OBJECTS ARE "PUBLIC"(THOUGH THERE IS NO PUBLIC/PRIVATE
HERE LIKE C++)
# BUT WE CAN MAKE A VARIABLE WEAKLY-PRIVATE USING"__" BEFORE IT
# EXAMPLE:
class human:
name="mayo"
age=19
t1=human()
print(t1.name)
#print(t1.__age)#-->throws error
#to access age we use
print(t1.age)#private in python means the interpreter changes the name of the
variable with "__" in use-->this is called NAME MANGLING and is used to avoid
overwriting of important variables
print(t1.__dir__())##provides list of all the methods and attributes of the
variable t1
####################static-method
class math:
def __init__(self,n):
self.num=n
def cls_sum(self,y):
self.num+=y
@staticmethod ##basically a decorator-->what's a decorator ...quick google
search;)
def sum(a,b,c): ##no need to pass self
return a+b+c
m1=math(5)
m1.cls_sum(10)
m1.cls_sum(20)
print(m1.num)
print(m1.sum(2,4,5))
print(math.sum(2,4,5))
#######################class methods
class emp:
company="microsoft"
def info(self):
print(f"{self.name} belongs to {self.company}")
@classmethod
def chng1(cls,name):
cls.company=name
a=emp()
b=emp()
a.name="mak"
b.name="shak"
a.info()
a.chng1("tesla")
a.info()
print(emp.company)#the company which was a class variable is changed
b.info()
#########################SUPER
# super keyword is used to inherit parent class methods from the child class
class parent:
def p_meth(self):
print("this is parent")
class child(parent):
def p_meth(self):
print("child's own parent")
print("child's own parent")
super().p_meth()
def c_meth(self):
print("this is child")
super().p_meth()
x=child()
x.c_meth()
x.p_meth()
# initialisations
class math():
def __init__(self,n):
self.real=n
def show(self):
print("no=",self.real)
class complex(math):
def __init__(self,r,c):
self.img=c
super().__init__(r)
def info_child(self):
print(f"{self.real}+i{self.img}")
super().show()
x=math(2)
y=complex(3,5)
y.show()
y.info_child()
#############walrus-operator
import platform
print(platform.python_version())#check python version
a=5
# print(a=6)#-->error as this aint allowed
print(a:=6)#-->now 6 is printed
b=9
c=a*5+(b:=10)*2
print(c)#--->takes b as 10 instead of 9
num=[1,5,9,4]
while(n:=len(num)):
print(num.pop())