0% found this document useful (0 votes)
1 views3 pages

OOPS+Python Session 2

The document provides an overview of Python classes, including defining classes, constructors, inheritance, access modifiers, static methods, class methods, and the use of the super keyword. It illustrates these concepts with examples, such as creating instances of classes, modifying class variables, and utilizing decorators. Additionally, it introduces the walrus operator for assignment expressions in Python.

Uploaded by

kgsagar88281
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

OOPS+Python Session 2

The document provides an overview of Python classes, including defining classes, constructors, inheritance, access modifiers, static methods, class methods, and the use of the super keyword. It illustrates these concepts with examples, such as creating instances of classes, modifying class variables, and utilizing decorators. Additionally, it introduces the walrus operator for assignment expressions in Python.

Uploaded by

kgsagar88281
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

class person:

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)

print (f"{self.name} is from {self.prof} and is {self.age} years old")

a=person() #an instance of the class person


a.info()#no more arguements needed as the class itself is passed
b=person()
b.age=22
b.name="Ram"
b.info()
person.info(b)

###########################################################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

def info(self):#self automatically suggested by pycharm


print(f"{self.name} is {self.age} years old and {self.no_person}")
# x=me("mayukh",19)
# Y=me("rahul",10)

##############################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())

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy