0% found this document useful (0 votes)
10 views

Notes - OOPs in Python

Python

Uploaded by

Debdeep sanyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Notes - OOPs in Python

Python

Uploaded by

Debdeep sanyal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

OOR n Pthen,

'

awhatis objec orwented pas m


****** *

Oje ek-oiendted Proymmmi n


***
' * ***'****
IS aou cretn
oljeefstaConfnin bsttdaa od funeftn.

Who is e stauelwre oljed-ovnded prog ?


***r'******'******* ** * * * * ** ** *** * *

2The stuehre2 ojee-osientel pro inhke . * * ** **|

alowinap
**********V.
' '

elagse ce heurdetned dua.types ho


acs e bluepaunt r ineividuol okjects
attributey and mehads,
Met-ods; ane tnefiong touane detned
inside ca clase t a s desCue the behoviouoy
M olojeek
bjeesane stanceA class. ceated uth
3pe ehcal dehined..dotea. kjeekg can coae)-
pons aexl world ohjects oan.akatrac e n .

Htoiotes are detnas in the elass termplate awnd


epcsen te zate s an ogjeet. Objecs
have deota 3toned in the aiubues rel
' ' *
. .. . . .

Whod is Class f
elass isa bluepuin sek st i inatuet
ics build a »pecisic Pe ookjes. I4ir **

a basis concept d okfeesoruetd-Boamin ' * ' ' "

whuch evrve* * ' * * *


w the aelte ensihe).
.

Stayo Class in Pytbon


elass Car I naalase. ' ' ' '

colo7blue"
model "s po
tdoba
In 8nakec
decaleulate_avy-speed(k,time);
ASome code.

1Dierence between 3tncture aClass


Class Stnetue
O16Jeelmel using::**class
** *
t1s deland usinp
. .

heywod stuct keywORd ***** ** * * **

Membery clasg ane Hembers grnetwe ane


*

Privede '****'**************
deflt *****

**** ****
puolic e deult.
(Cecupie mone. APace. Cccuple.ess Papacc
**** ' ' *

Obieeotackoss 0biecf tor a 3tauetwre


*1**''

CAEtes in the hep


s ceel in tha slaclk
memo , em .
* *
**** *** ... . ***'****'****'***|

OLeicedgCaeed Ma on mat notuge


wingneu" eyxord."ne kepud ule cating
ee

***** ****'*****

2imiloou hie berwren 3ruetwre Clast.


.

Stsuueera ****** :'


ae yetkuetopes cms ecsegane
Aeterere tpes * *************' '*** ***'*'* '** ***''** * *
B t h can have conshuchos,methods , P-19pentre
tfelds, censtnt; ,eneumeats, events,
Stekwes and classes can implemen
3
ntertece
Both he can eve constuervy w t
AeM
aMd cUHhg posmeter

3Accex Modifhersghces mattrers pley an


impodtnt nde t pretecf he osa, Trem na
a e accesp. well as etecrinitom,
getting manpuGteshe ace dities desne
ow e mexbers 6ahe clasg caM e atceBed
Theree type aceess madhioy ane-
.

* * *********** * *

OPallie2:
Pteeted
***************

Palelkc oe deelared pulelicly ensilg areyie


om anL r k fhe peogra A de membexy
deaslr.
and uncong a r e Publte b
ivate member ane deelaned psuvate, are acexible
udn t e dass ony pruacte acey modten is
he most 3epwre acees moaiter. Declared
Adding a donde underscone snbel betare
#propereties/dada m e r .
F-nebonali/membotumetin
oteted Only acessabla a claas seruved rom
t D wembers adass.ane deelared.
Ptecded bu addine a simbe. deru>calte
befine ke shla mewnber
'***"'**

Synde»
Class Layi.
tlang="C." . ****'* ''*''**"****

-secondn4= "Pu/hon
-Hind Iamep Java
Punng.ftasle )
pun Lap-3econ
Aunt (LaneyLem-kiadla)we on acces pouiafe
MadhiA.

Menber tumelion funehians hic dete


Aebeariaur. ot:an dajeet.hot.closs
ane calles... member ttn ekona. ....

* * * *** **** ''*'** ** ****** *

Tnnerr cass;Clasg inside


. ****'***
. . cas
* ** ** '*'****''** *

Constueray:he k sconstauetuay
isto inialize(amign value) to the, data.memkery
ot he eloos whenanjees. he class.
ereated n ekan -init-) method Ls.collel
****''''''* ''*******''**** ''*'* '***'****'****'****** *'* *****
t e constoucw and is calles when an
okject ls oekd.
Sntay
eefit-oe):
*** '''*'**

etul Cangtuefve Is asinple constuneks


*****''***'''***' '****'****''''*** *'***"

cauc doez f t ocept akap apment.ts


dneon haz only on t * ******
oament which ia
aeferenie to the hsfance kein eongtucted.
.

oamekeruzed Covstnc
* **** ' ' ' '

? Ita r angune
S.9eTEAEnce eInstance beinge Constuceled
knotal'nas 3e).ms the 7est the aparMeg
One Pwided..ethe engramen

Wheneve we cAexted..n okjse ot.anse closs


the detaulE.constzraetorr. Is getti Colle
tts it3el. * * ***************** ******

w e can use the bui-inchssmehos deeonahr


towsite multip!e.onstuedtas
.

' ' * ' '

Destuueton Destucrar is q memberr mefhed da


cla ss delefes 4e memae 6 e deject
--del-U
T2What is an
Objret?
An okjeck is an 'instance aelass. A claos is
ke a bkeprint uwhile an instanee ua
copy o
he closs h aefaal yalnes. a n is objeof-0otir ted
pominp. lanpaaqe ttatt 1rey an ohjecka.
ma,emokasizes nedions
Oj My Cns)
pouin&-Cohi )

81 terence betuueen Objeek d clae,


OLeek Clase
OOkjes an insfaee OClass t a bluspunt on
o a clas s.
'''''*****
Henpaie 0 uieh ojeeig
e eeoted,
OOeeka are neal iwerl9Clasa ta gou a
entitp, 9i ahjeets.
D plypsierl evdlp 39 logcal endil,
ENOking in pten ic Peeared wi elgs kpt
an sjech. O
**'*****'*'*'*
|-nd e42 class Shuden)
Ojchs alle cales ela9s doe allecated
mema
'
10f when
''*''
wemoy- hen tE iR
' ' * * * * * * ' * * **

eteotad .
Eakel,
' *'**
3/Metods The Faoypose amehoe is t Pocess
e vauahlez. paovdedina elase a in the
nekhsd. Te Mekhods
Tnsavee melhads. **''******' **** *

. Checes sart inebads.


Mutaton metedz.
lass methos
******'****' ****'' ***T''

3lahe methsh.
**'**'*********** *** * *******'********'**********

3 nsfane Meods 8 nstance metods ae he ******'* ****''**''******

methads wel.act.upon.he.insfance.varades
e dass. Tndance methada are boun
instance ( okject) and hence celled a
sfaneename. med C. Wkale CaXtnse the Ktance
metsds,we ne ed nt Pas yelne
hesef vrable
Aaezsa Methsis3E.SimP ..CcesSS. Aead
dloda di he. varuaea The..de net madiy the2
dedes in the vniaelea also edllelas gete
mehads. der geSName (Selt) '**'

e o n selftiome
34 * * *

Mutbtoz Metha Arethe.nethadswheh nat


anl e the loda but also nad
****'* ****
them
hey3o called ageter metkods,
* ''*'*

def setNome Cset, name):


' * * * * * ' * * * *

sefame name
7
3Class Meld: Ciass meeds ***''**
are
****''**'*'
the
**°*''*'''*
mebeds
threh act on the elass yarualsles gakie variables.
These metds are wten usinge easgmehd ***'''*** ***

deeogtatae aoove them. *************************** ****

33tatc Methad2State methsd a e wes ..

hen Scme.processingt Is aelote..he.


alass..bu.does netnees the..clas. ar tg.
ns.kamces.. exdoJLm.an. workSafte mecs *'**'***''*''****'***''1****'

ane tten hadeeoratm.@sdestiemehsd


a bovehem
*********

****'''*
' * * * *** **** ****'***' *** *** ** *** *** *****
******** ****:'' **' ' .

kedunes aO. *'''*'*** ' '**'*******'*** * * ' ' ' * * ' **'**''*'*** ' ' ' '' * * * ' ' ' ' ' .

oymor ueThe0d olmophisn


meon3 havina.meamg vi ma aimä. A ''******

PAODOmm po y Maukis.Mennsthe 3ame


nconnameY u diterentRUes bernop us ee
diterenTaypes. ***
***'
'*****''*'''**' '

SyntaN ybe sed da * ' ' ' ' * ' * * * ***

lesunt(le Sow stun.


)#6
#lenl! bem uye to a tst. **** '
*'''*''*''*** *****
***' '**'

ppinlon(C1, 20,39}))+3 '**^

'******
****

* **'**
41 Tmcfbng Overdardi'vapa Funettova overkeodin
stheFAocess when te ame fn eHons an e
uee mulliple timeg bassne O ddteen
nambe oamefey as argamens.5Ptho
doe not suppatuneons oveWmedinp An
eoa Aes thnown we implene heduncton
averedinop
.

bu does ne
.
man thee is no sther ******

to implementthis. We sil desine suneeyg ** **'**** '****


t e s as None.
setfivef_One
* *
ai nsne pa
* * * * *

ass.Mame
de nl3sltet-tNene,P=None, .

Meltoda undel meod


1 P u n d e ox. Magte * ****** ** *******'***"

inpthon..ane.the.. methds. hamn uo preht ans * * *

tiNanderseane i he methd name Duder.


hee meanyDuble. Unden ew examplesne
int cddjlen, -Xep.

operot pervsnma. addikona aek0ns thehom


what
i415 meantfoa, it is callec operaton overuleedin
Operutern Nerdeadiing. is. en eeple.fo paymriphi'sm-
OpeotR owroBndinp. Magic Mekhod

oeekaddla-,othex/= jert idiv (sEthtAer)


' ' ' '

ojecadd_(selPattery e t HodielselP,otker * *

*jec-.
aothe)|%=oject imod(etöthormrub|)
jecgecd (>elhher) **=ofecip(selG ther)
*****'** '***''

jecfAnndia.(oelf otter otjett(se),son)


dejecmd-(seb,dher)
* '************
dlec-le (self,othen)
seec-p-gette1)
kerct-iadd-lself, other) jecfe-(set, sthery
ee(see, eder)
jcchmul-tpelf oey != secf ne-teP,diery
**********' *********

41Method Ovorleedig Bleorphismi Ta metyd


-

iswdten. ueh thot c a n petin mare hoa


Cheak..1t.ls.aled, methad overloasiy
6 Melhod Ovewiding morPhe hen-there
Is meihod.n 3upen class ouritin he same.
mehard.inthe 3uo. class >tha. deplace»tte
2upe class mehsd Iscalled. mehord oveyidihe..
*** '* *** *

. . . ****

**** *'*'***'** ** ' * **

10
elases rom the
Inheritance e envínep
N
new

eoistinap elasseg such ttot te new claeseg iheut


althememberrs a existhge elares.is calles
inlneunce
class Pseclos:
* **'****'**
*''*

* *
clas Peuvaloss (ageCla)
T ' ' ' * * * * * ' ' * * * ' ' * ' * ' ' ' ' * ' ' * ' ' * * *

- TErd
***** ***T''

-2ingle BenileeDeruvim .
one 0 nee

u clascCx dom a sinyhe base..is calles sirgebe


ineritance n Smgke inheukance uwe calnay a e
Only
On dne a e case, buttheedsss me Con be

n number O closces deived toM

Makiple Interi kamce Pouiriz 3us clasce


'*****'*******

12sm * * ' * *
multiple base clsges
* * ' * ' ' * * * *
is calles ruulHple ***'*

ineristemce Thae uul be moe thoan oneupen


' * * * * * ' ' * * '

Cmd here ma ke gne o wor 2us cosseg.


co s * * * **** '*****'*** '***** '***********'******

. *

G3The super Mekbhod supe) tea buitin mehad


iss t ca he u p e l eal class constxo(- **'**** '* ''*** ***'*'*'******
***'

efort ah nethads om the sub elasg. hngp-


constuetan witen..in fhe 3uper elass is not
ayeiloble he 3ub closs the ab elocs hay
****** *****

11
mefasd
per ***'**'
class consue *********** '*** ********'***
sino the super

A7em inside. fhe u ebs congstuet Sup


ailt-in mefhod in Mon thot tontain he
* * * *
* ****

iso super ett class methsd ************ ****: ********* .


**

Super) Conn we s
Supex .-init-C) #call super ekllaclees comtnetn ... .

3uperi nit-(aanent ) e ghme


******** *: perC.methsd). #tcad supern olass. med
******

4.94 Dtterenoe between Inerutce. alt mnphsr


* * * * * ** * * *** * * *** * . . .. .

Tnherbtnce Tmpkism
OPplel clasg eppltel t metnds

T upeste caneept DTtallou


. . . .. ehe oktecs
eusebiike oms tdecide s
eue 21duces Cde ewncian t imple
length in O0Ps men * *********'*
a ompile fime ay
a un ime
******1 **'******* ''***

T can be snghe )wherens tCan e


hoid mltphe Compled-kime Palymrphsm
A ieunehieal andCovelord) as wella me
muwlevel inberitamcePmegg(overodi)

**** *
4Eneulaliou I descubes he ide d o
appin de ndhe melhrde thsu wo
on dodawiin one uniThs Puls restuchbng
on acceing vauiableand mePods dineeg and
an paevent the acidertal merdnicaon å oaa,
**** * * * * ' * ***

********* ********* ** '***''*' **''******** ' * * ' '***' ***'***'

Bd vantages 6 Erapsokon The


adaantgea st.ugine enop.3ulahon.Is.the
sec DotarT ratecks an obec
nauhariredacce. Othen advandege
ae.Doa Hding, Simplieikand.aesthehts.
Clase mewber Gecers 4om Accesble fon| Acegible
****'''*
****s

accoy 3pectkiex| own coss dervel claos rom ajecti

ile Yes No No
Pruteehek Yes Yes Nd.
Pulaie Yes Yes Ye
' :

. ..

''*' *'''*'*
' ' '''

Aatzaehtr In eutlon,an akstaction s


s e to ide the imelevan date/ class in.sder
to edhce the gmplexte E al enhomcey
the apPlicahonetcdencd
* * * * * * * * * * * * * * ** * * * * * * . - . . .

Ighon povdesthe abemadue uße.


the he abstraetion in thc Python.proam. .

no ac jmpoa ABB
'**'''''*'*''*

elasss ClasswameCAEC)
*'''****'*' ' ' ' '*''''* **

Dferene betven Alsraekion Enopbti


**** ** ***************

AbstraeHon Encapaulalion.
O Abstroetion wOrks on. O Eeopsulotion uaks the ' ' ' * * * * ' * T *' ' ' '' '*** ' ' ' ' * ' ' * ****

the design ewel. PAlihon evel


T * * * ' '
ipemenles t 2)T
. ** '***'*'**
the meehansmo
Dide he Unn ecess cw d ing & din '*''*' ****:
the cde
Ond the dalla,
T h l g ks uhw T eue on the imen
*
Ts he work d chjeetekel hes the objedts
* * * * * * ** * * * * * * * ' * *
..

Instend Aou the '***' '''*'*''''**


Is wo , '''*****'**''''

ojreb woLg, ' **''** ***** *

''' '****'** ** ' ' *''**"*

14
BMise Guesthiom on 00R In R{honL
5 DHerence bekween Bocesural A otjecdes ** ******'*

Oruenek prap-avmiu
ocedoo oprsmmu, |ea0rferdted Popa z
i s e s s seqae. ***"
ata din s oaible
than OD
******
***''****** '*'*'''*''** in Oeckotedes powu
''''**
****

Y''***** *********:

due to akbachon
**** **

* * * Tolos botom-np
CPPach Ppreachy

i s suelwe-aiented) * *
is okjeet-asienteL
****** **':***
** '****'''**'**'***

Tene ane ne acces c e . modifitg in 00P ... ... .. ***

mestss. Qne puvake, pulie . ....

Pteted, * *** * * * *****'*'*

No appopiate Appoupriete tot coee


'****

**'*''*** ' * * ********


Poole
Cope*
elem
*********** * * *** **** * .
. . .. ****'**''

****

*''***

Faple: e,Poeal ) Þaple: thn, Java

2whains ota prepammin Jongoage


Phonp ..... .

Pyhon ls basieo ** ******


abigh-Level OkjeeA-Choenfe
******

15
g nn(atage, If also en înterposted
anaaage i ues ine-bfline ex

Wh ts siite in Tuen ?
slte s etthe a gimple stateme in one

Une cs it is a newline lowed an

indenfed bleck which consists & stements.


Wha is nit keynd in Pen
The le leks the Peton interprcr
Kno that a daeety conteins cde t a
puThen o d e c a n be blank. LWtheuane, y
annet imps mostdes tom another trdern

The int mehs tg similan constrchrs


in Ct d java.(onstnc eary ae us ed t
ins li2e the dgfee se

Pifoence bekweon Mediles á padeage


in Pythen
The mesdule is simple fpon ile tto
conrins aleerons t unttons and globas
vaialbles and h avingap extensio
ile
The Paclkage t a
inpe dserry vi
* * * * ' ' * *

Cllechon
' ' '*
modules he Jetoy contarns
**'** *'*****'*
*******''*' ''"*

thon madwles ons asoowininitP


le ueh he mterpreter ider rels
clkae * * * ' ' * '* * ' ' ' ' ' * '

* * *

Wha ta 'el} kespsars in


**''*****'
honuP
' ' * ' ' ' '' ^ " * * ' ' * * * ' ' *
*********

potomefer e t e e n e E to
he * * * * * ****
a

theCOUent
' * ' ' * ' * ' ' ' ' * ' ' ' ' **********'*
hsnce. *''***
h e class, anAl
a n d s . s e d o . acce.VAuaLletat

belonga
**** ********* he.closs.
PYerene beueen Rekin and
* **

T n PYthon.tte piekta pchle.msdule


Aes.epts. n Lon.oicet trtarumg
E d O. une e p e Rndedo.ad
deamp into. insthe the dmp
A unchjon:.Ts PAecesk known as
*****''***'*

pielin heAngtton used l s s gee


.spicledump). Mso, calle sertozaion ....
The proress d eterit te origk0 dda
The **'''*' ***'**** '******** *** .

Pyhon okjee fro-he ztored


epresentaAons '*** **** ' * * * ' ' * *'**
is aMed anpickkinThse
anen sed othsprseen. Ya piekle.Jond),
b caMes de- leRzatrons ' **'*****'******** '****

1
Wha does k s and * Kuna mean?
Whe you aie not clear bou mane
agumets you nerd to pass to paKeular
func tonS, then we Ae k a n and kwor
vaued
hemgs akewd reptesenbs
a

numbeo N cogment. Ttis used add


toethen the vnlues a multple men.
The k*kuwar keywand aepresenls an obito
number o ophmenta thot ane possed a
tnciong. **kwanq keyuords ame stone& in
a dictoneoy
whois Opew""With otakemeun pae
B o ztekemendg ane usek în case & Atle

hond livg
10wWhat te hon p 3
PYTtONPATH ts an enviromen vasvalle
whtch you Can set to add dditiovo
diettories whee Pthon ul Saok. o
wodules and packages.

congeu'ns he hytetvde d youpaof


E1halous Cop d eep oP.
Shoo Copies dupltcote as JHle as
possible. Caeating a newcjeckand
hen coTTT the..mon clds h e . .

wnentoje the. new objectE


Deep cap dupCke eV u . T s cnni
****:" N
anew ojec and hen Copyi he non Btke
. . * *** ****** ''***

reldls d the ewwent o j e t t e neu opje


* * * * * *

.. .

. . . .. . ... . . . . * * * * **** **

********"***'**'''*'***'*

. . ** *** * * * * * * * * * * * * * * * * ' ' * * * * ' ' ' * * * * ' 1 * * * ' * * * * * * * * * * * ' * * * * * * * * * * ' * * * * * " ' * * * * * ' ' * * * * ' ' ' ' * ' '

***''''** ''*****
****

*** *** ***'**********'*** '****

*'*****'''''*** ****** *** ' * * .

***

19

You might also like