0 OOP-5 :-
An oL:-ect- O'lSienfed pa'6Q.di3f1Yl ..\-ob ~S'eh ~ ls
~ clo.sses OrnCJ. o ~~c~s. lhe t
obieapt~~o;m
"' b I.J 1..
9 +o~elitl' ~eo.\-Wo)s'ld ~~i,tres .S,uch
e pencil1 sf-c. oops
CtS oo,, 'lous :...N\ wcri+.m9 -Hie.._ creusa.ble.
CChcept focus.es '7"' · _ r OOP.
ecJe, . Mqjo'l( p~i (Y'c i pies E,)j" :s .-
> CIQSS > O~ecf ) Meihod > J;, he~i+amce
> Po\~n-o0~phisr«1 > Da+~ Abs+~o.diorn
> 'Emc.apsulo.·h'o(Y)
Evecr~thim9 im f1-\\iora i5, am o~ect.,, omd alrmo~
eve~~1him9 hoC:a. a-\\~ibutes amd nne¼liods.
A\I fomc1iom~ h~ve o. buil+-i«; oJhnbv½e ---: doc._;
which 'lS'e1o'lr'ns -+he d.ocs4..,m9 cldic-ned 1m -the
.furnc-tiorn Soulrce c.ode .
\vl-iw we c~ecde doss.,, it neeols -lo
c~e<=ite o.m C9bjecf- -to o\loccd·e -the. rrneimo~j .
t
doss CO."?!!
J.J _ ·o-.·1t - ( selt,. <'r1lode\V10.Jme, 'de~) :
sel+. rmodeh,OJW'le = rmodeln0.trne
self.~~~ = 8~
dJ d.ispQCj ( self):
p?rimt( ~eH=: cmocl.e\ ho.rrne I se\+. ~eo.,r- )
c.,1. = coJS ( '' To~oto. '' J 1oll>)
ci. cli~fl"~()
# Met-hod :-
lhe C'fflefhocl is o. +ornciiom is ass ~ciaied
-\-hat- is o.ssoci~tecl wi~ Ot?jed-.
We corn c'ISeQte o. c\QS~ which u~es a\l -the
pcropeY\ies amd be\,Qvlo~ of D.Jno-lhe~ d~ . The
hew clQS~ ~ l<moWh o.s o. de'IJiveol do.~ 0¥
c;hi\cl doss., Onlcl -+he one whose p2rope2r\.i e.1 o.¥e.-
o.co..ui lS"~ if> l<MovJm Oh o. bC\Se. clQS.S 01 pcJ-e,J- ·
Evicapsut~+lo'fl p2rcleefs 1 cs~e.cts sto.tes
bj <'(f)Qkirnj sorme
o.it~ibutes p~1vo.Te. p~ovidim.9
o.md. public <met1-,od.s -1-o
c.ess o.md rmodi~ ~em--, • Ir'ri advo.mceo\ usage/ ~ou
o.c rrni9ht use. pa-ope.~i~ cleco?s-Qbs +ocr clea.m~ o.md
m,o~ P~-tt,Jo\1lc c..od.e. .
Co.11 :
clef _ iefli! _ ( self., rroQke., rrnodel., 'd ea.11') :
se\+._rmQke - OflC\ke. =II= Hro-teclecl A\hribute
rrnodel #= Pzro+ec\ed
Se\.t.
- 'Y't'lodel .::.
'deQ"lf # P~o-\-ec.+ed.
Athribu1e
Atb,i bute..
@piope~~~ @ p~ope?S1~
def rrnoke ( se\f): def l3e~C $elf):
~e+u~v, sel+ ._ooake ~etu~h se.lf._~eolf
@rr(lake. sette~ @l;1eo:zs-.se.ir~
def rrf\QKe ( se\F., n«l\<e ): clef ~eQ'IS ( self., ldea.2r ):
Se\f._CTOake = fWlake if 'deo.1, > 1&85 :
@p<fope'lr ½, self._ 'jeQ'2:f =- ~e~
chl rrooclel ( $e\r= )~ e\se:
lJ'Q\Se Va.\ue Ds-lr0'2s{ '1 -
cf~6h
@rrnode\ • se-\t-e'a'
-
self CWloJe\
- '/ea.¥ <W'l us+ be
~--..e~+~ ~<M'I 1&iS1~
clef: <WloJe\ ( self=., (')'()oclel ):
self._onocl.e\ : cmocl.e\
:/1::. U&,m ~ -H--e C.o.1S Closs
C~ : Co~ ( " ies\o. ''1 ,, Hocle\ 5 ,, ., 2021 )
pciimt ( Ca.'l!'.rmo.\<eJ #- Outpof-: Teslo.
CO.'ll' -~ea.~ ::: 2023 # This will upda1e 4e ~ea.~
p'c)irnt- ( co1r. ~ea.¥) # Output: .202~
2o Abs+ra.c..-\-ioN">
Abst'to.ciiorn i~ about c2re.o.lim', Sirm ple I clea~ icn1eb'fuc.es.
amol h,ol,m9 1('Y'(lplen-nemtQl'.io("(l de~ i\s . Vs,m9
P~'"1on1s l abc. 1 miodole., 'jou co.rn em.futce obst~<lc-~01Y>
b~ c:reQ.1fcr'.9 abs+?re\ct bQse classes.
""h-ocm o.bc icmpo1rt ABC., ob5>t~adrrf'>eihod
dose; Shape (A&.):
@abshroc+rmethod
cl.et O¥ea ( s.e\ f:') :
poss
@abs-hractrme-+hod
d~ p~aooete«r ( se\t' )~
PQSS
doss Rec+~9\e ( Shape):
d~ _imit- ( S.e\+,, wid-lh, he,9ht ) :
self. wicHh = V'lidl-h
&elt. heigh~ = he,8\,\t
d~ o-isea.( self):
¥etu1rn se\t:. wid\.h = selt. heiaht
d.e.t pe~irrne+ecf ( ~elf=)•
isetuisn 2 * ( £,e\f'. width + se\~. heig~t)
# Abs·hro.c+ d<4sses CQJnmoi be ims.\wn+,ated
:JI: shQpe = ShQpe() ::/1=- lhis will ~~e OJr'\ e'l:l~O~
:II=- U~im9 +he Rec+o.rn9le clos.$
"2S' e ct - Rec-to.m <ole ( 3 1 4- )
p~im"t ( 'c5ec+. o:aeo. () ) # Output : 11
P11irf'-t ( erect. pe~kmete~()) # O utpu+: \'t
.Irn'ne1si-\o.rnce
~o.+ sha.)'e.o.\\ow~o.s-too f
+ "6ectcti [~ o. h'1 e¥Qo'ch~ cf cl~ses
Acl.vC>JY\tecl usa.o.e
, u
~. e ~ _ r+~em. <? }i butes
\rnvclves
arnd <'rYl ei"t,,ods.
UMSe~\.omd.·(Y"'IQ ·
n. \ ~ supe~O icl rrnu\.\ip\e.
1
irnhe~i+o.mce I i ..J
r<.eso IJTIOIYI Ois- e~ l t-'\RO ) . ornd Melhod.
C\Qss. Ve hic\e ~
d~ _im\t_ ( se\F, r,nQke, rrnoc:J..e\ ) :
$elf. rmo.ke :::. 011 aKe
se\t. o,,ode\ ~ n-ooclel
def S-to.)rt (. <eel~ ) :
~etu~n + £ se\~ rroakel
11 { self. rwiod.e.\} is
C\Qs-; E\ec.-t~cVehicle ( Vechic\e) :
d.~ _,mi~ - ( s.e\J: 1 <1'<'al<e, cm ode\ 1 ba\te<I""~ - si2-e.) :
supe~ (). _ ,mit-l rrrial<e, 0nod.el)
5e\+. bet-t\-e)r~_sii..e - bQtr~- size
M s-\-a.1!t( sel~):
oe+~>" .f 11 [ 5e\r<Yt\Q\<.e} l se\~.onocle\} wi"' {self.~~..s,zJ
is 5tc(,r'tim9 s,\ern-\-1~."
# U:,in5 -\he Eleci~c Vehlc\e c\o.ss
e.v = Elec.t-ric Vehici e ( '1 Tes\CA ' 1 u tv1od.el :, '',, 7'5 )
1
pA1it ( ~v. !:>btO) # Oul-pul"• Tes\Q Moel.el ~ wi+-1-, -=1'=>KWh
bc.-tt-~ i$ s\Q~m.9 .sil~tl!1 ·
G
P0 \';1rmO'b' p his.nt'l di ~~e1S'em-t
allows c\asses -\o be 4~ea:ie.d as
~ -!-he
ims-\-OJ'flce.s 5°""'e clo.ss -thcrou.9~ a
Cormll'noro im+~fo.c.e . 7his is- especff ~ po\,J 7lr+ul i(Y')
odvo.roce.d o..pp\ic:a.iio!'1'S. such o.s d~t1la.C'd'1C. oneihod
lmvoco.+iom arnd d.esi9mim9 +lex,ble HPl.s .
Closs Ami(Y(la\ :
cl~ .5ound (self) :
1S"o.lc;.e No+ lrmp\erm~-\-eol E~ro¥ ( 11 Subc\<45S 1r11vst irl'l\pleKY>en-t
o.bs.\-~~ct rrnethod. 1\ J
C\a.s.s Do~ ( Ahirmo.\ ) :
dJ 5ound( sel+ ):
1S'etvrn '' Ba"a'k 11
Closs Co.+( Ani(l'(lo.l ) :
dJ sourod ( s elf) :
~efo"lrrn 11 Meow 11
.# Pol~~ehic
- - beh"vio~ :-
d.J cmo.ke_ soumd ( OJ1li('\'Y'lal ~ Ani('('()al ) :
Omirrt'lo.l. Souc-nd()
omirmo.1$ = [ 0090, Co.t() J
-k>'b' arni rmoJ im um)<rr, oJs :
pcnrn+ ( rrf\o.ke_sound ( omi(Y'() QI ))
C\Qc;S
0 Em9-ine :
ole.f Sta.~f ( 5e\+ ) ~
11 c__ , e.
DTI<31m s~tecl''
Class Ca~ ~
ol~ _im•,i_ ( self:/ rrno.ke 1 (W)oclel ) :
se\f. rrnqke. = {)fl ol<e
Self. rmode I = cm ocle. I
selr e.V19ime - 6-9ihet) -# Cormpo~i-¼ch: Ca.1r
Co'ni0\i('t1S Q/Y'\ £n-ie;1he
def ~+Qcrt ( s.e\F):
creiu)h + .f. se\f.rmqke.}
11 {se\f. model} - [selt.~hj·n,<.~iO
-Hie C\Q.ss 3-"
ca.~ : Co:~ ( u 'Toyo-to. ,, i " CC>Jm~ i, )
p'b"imt C Ca.'cS. 51QYf() ) -# Output: Toyoia. Corrn~~ - Eni~\'.,~e
~~1ecl
@ Cla~s. Ern9kne:
de= s.\o:<1t ( selF) ~
cretu~m 11 E.maine Sk, ed. 11
Clnss ~:
def _im·,}_ ( se\f., rmQke.1 rmodel,. emcaime. ) :
se\+. mQke = rtnQke.
w self:. rmodel =- rmode\
# A~Cj"Ce'30.liort1: CA.er
hcts ON) Ern9n--i e..
def s~t(self):
-iret~>'rm f" l. sEf. C'f1lC\l<e} { se.\F. rm 0 de.l1 - {self. em9ime.. s~~>~O i
:/I: Us.,ni~ .\-he. Ca"as class wi~ R9c.31r~'llbrn } ..
ern9ime. =- Eh<3irne ()
c.o.1r = C0Jr (. ' To'j o+Q '' 1 '' Corm 'cs"~ " 1 em9 ,rn e )
p'rirnt ( ca.¥ . swt(J) # loyo~ Co.rmis~ - Em5,ne Sto.~cl
6 0 tvle\\iod Resolu\io('(') (ftde~ C. MRO) :-
MRO is -""e 01rd~ irn which P~r\-hon looks -to6 a
rroe-\-hod ,rn o. hiecr0t1rch~ o.f- C:\as.se~. This i.s
es pee. ia.\l~ "?S"el eVOJv\t im f'W\ul+iple. 1m'ne:a rte.Mee Sc.e~q)-1os.
C\Qss A!
ol~ pcrocess (_ se\F) :
Pero c.eS5 'I rt'\ A,,
"6e"tt>~ CY') 11
C\oss E>lA) :
cl~ ptrocess (_self)
e, ,
b"e+o~tf' '' f¼-oc.ess ',m
C\oss C( f\) ~
d~ p~ocess- (sel~)
ye-\-01rm 11 Process 'l(Y'l C''
C\o.ss 0 l ~, c )~
pQSS
J~ Dl)
p~mt l c:A. p"b'oce~sl)) # Outpu'\: Rrocess ,m B
ptticrr\ ( D. W'1¥O t)) # Output: [ ~clQSS '_rrno.im-. D' > .J
<cl o..ss I
- rmo.i• m_. fl..l)
u ~
<c\Qss ' -<"fl~lr()_.
, C' '/ J
<clQC$$ I -rm~,('(l-.
• A' ......
, ~
< c\Qss ' objeci' > J
o Jin +his e,xon-np\e., +he MRO +ol' c\a.ss 'D is s how,n., 1mcLccd-im9
~~t •~• 's ()'r\e#-,oq will be cq,lle ol b~cre lc'•s
CW\e-\'hocl, OMo\ so on .
>> Oe*e°'O't\ime~ -I-he O'lcle<r irn which
\ookeo\ vp WheM' S eq,1Schim3