Python Classes and Objects
Python Classes and Objects
object creation
Python is
completely object oriented language
i e data functions lists etc are all objects
ext 1 is object so Datatype int is class
All datatypes in python are classes
ex l 2 is object of datatype float class
ex 2 1
Print typeca
class int
object here
ext n 1 2 3
print type n
class list
statement 17
statement 27
statement n
syntax
object name Class name arguments
simple ex
pass
ob
Firstly cretins atones
object that belongs to first class
ob
print type
If we consider mobile
attributes 2 Cameras
charger port
screen
If we consider car
attributes 4 tyres
Airbags
doors
dashboard
steering wheel
humans class so
blue print so are from a single
Similar attributes
2 eyes 2
legs
I nose 2 hands
2 ears
so attributes are variables to which we are
ext eyes 3
ears 2
called methods
In Jupiter Notebook
apply ex l 1 2 3 4
l
double click
tab
function
init this is a magic constructor method used
to initialize something
This is a special constructor which will be
default of this is
Human Being
white
brown y instate
but different objects
objects
instate attributes
objects
pl talk C
I am black
P2 talk C
I am white
P3 talk C
I am brown
P2 Walk C
I can walk
ext 2
eyes
nose 1
ears 2
hands 2
Class Human Being
class attributes
2
eyes
legs 2
hands 2
instance attributes
Pl talk C
Pl eyes
2
has to be consistent
when we are
calling
class Cl
O CI C
internally object is created and stored
in memory For this reference is self
O walk
here self is redirecting o using the
reference to access walk method
ext Pl Color
legs 2
y
hands 2
instance attributes
accessing attributes
pert eyes
2
per 1 talk
23
ajay
if we want to change instance attributes
Pert name
ajay
Pert name Rohan
pert name
Rohan
per 2 name will
Perz name change
not
Jay per 1 name changes
only
want to change class attribute using
if we
object
pert eyes s this just
is a temporary
per 1 eyes change of attribute for that
5 object Here class attribute
eyes acts like instance
per z eyes attribute
z
if we want to change class attribute we need
to use class name
Human Being eyes 10
pert eyes
10
Perz eyes
10
add attributes
dynamically
when we are dynamically adding it will first
check if attribute is already present or not
height 170.0
pert
newly added
Pert height
170.0
per z height
X Instance attribute height
Cor
only added to object pert
pertief
per z
easy 2
attributes are properties
methods are functionalities
Self attribute name is the instance variable of
instance attribute
Methods
Instance methods
class methods
In Python object oriented programming when
we design a class we use the instance
methods and class methods
variables
A Instance method is bound to the object of the
class
It can access or modify the object state by
be autom tically
created as an instance method
ex
Creating objects
perf Human Being ajay 23
pert talks
I call instance method
I can see
Pert see C
I can see
Inheritance in Python
In python we have a concept called DRY
which is Do not Repeat Yourself
se for example
class Human Being
def breathe self
class Animals
def breathe self
called Inheritance
parent class
G
or
def eat self
Base class
a s
Single Inheritance
multiple Inheritance
multi level Inheritance
Single Inheritance
A child class Inherits from a single parent
class Here is one child class and one parent
class
parentaass
child
class
multiple Inheritance
In multiple Inheritance one child can inherit
from multiple parent class so here it is one
Parent as Parentaass
child
class
ex
class PAI
2 1
def fi self
print hi
Class PAZ
4 2
def fl self
print bye
class Cht PAI PAZ
pass this defines the precedence
of PAI over PAZ
Ob1 CHIC
Ob 1 N
I
Obi y
2
OBI fi c
as PAI
function fil has
hi
precedence over Paz Junction tic
hi is printed instead of bye
parentaass
childass 1
childass 2
method overriding
when a method in the child class subclass
class P
def init self
print hi
class CP
def init self
print bye
D CC child class method
bye Class
overriding the parent
method
Class P
def init self
print hi
def fl self
print p class
class CP
def fl self
C class
print
Class P
def init self
print hi
def fl self
print p class
class CP
def init self
Super C init C
print bye
hi class methods
parent
ye
class P
def init self
print hi
def fl self
print p class
class CP
def init self
print bye
Super C init C
O CC
bye
Idgaf fight
hi
class P
def init self
print hi
def fl self
print p class
class CP
def fl self
Super C FIC
print acclass
O CC
hi
O FIC
p class
C class
ext super C fi C
method overloading
Two or more methods in the same class have
the same name but different number of
or different par meters types of parameters or both
Class p
def fl self a
print a
b
def fl self a
print ab
q PC
g ft
error parameters2 as
Expecting
and method is executed
q fl liz
I 2
class p
def fl self a
print a
b
def fl self a
print ab
def fl self a b c
print a b c
q PC
q fl Ci 2
method overloading
ist pip install multiple dispatch
Install multiple dispatch library If it is already
installed or once it is installed import dispatch
module from it
2nd from multiple dispatch import dispatch
Here by using multiple dispatch decorator
dispatch we can make method overloading
to work
Class p
dispatch int
def fl self a
print a
def fl self a b c
print a b c
O PC
O fi i
of dispatch decorator
I function
O fl 1,2
I 2
O fl 1 2,3
is
O fl 1 2,3 2
studio
ex class P
pass
q PC
ex is subclass P object
True
So all the classes in python already a
True
is instance 1 2 int
False
polymorphism Advanced
Abstraction
encapsulate