0% found this document useful (0 votes)
17 views18 pages

PP Unit-3

The document discusses the fundamentals of classes and objects in Python, explaining concepts such as data abstraction, encapsulation, and the differences between class variables and instance variables. It covers various methods, including constructors and special methods, as well as access modifiers like public and private members. Additionally, it touches on built-in functions for managing class attributes and the concept of garbage collection in Python.

Uploaded by

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

PP Unit-3

The document discusses the fundamentals of classes and objects in Python, explaining concepts such as data abstraction, encapsulation, and the differences between class variables and instance variables. It covers various methods, including constructors and special methods, as well as access modifiers like public and private members. Additionally, it touches on built-in functions for managing class attributes and the concept of garbage collection in Python.

Uploaded by

23eg106e26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 18
Orie Th Ua Dale: 25-11-2094 oy Tharsd aug class: A class Creales anew type and an objeck ce Sey Anslance of class class provides blue print oo lemplale using which objects ave ciealed, Ae python everything Ss an object or an instance of some class. Ay O=10 (7 abject ye type Ca) class Mal'> Sper: class class -nome : Sta Ska 2 abject Syntav- object mame = Class-nameC ) — To acces! member of the clay Sinbar: objeck_name- class_rnember qe obj 4 =00 D oby A va. ol: {me on Dala abshaction ¢ dala encapsulabien Chiat —_- ing Dale abstraclfon refer te process by which dot, aad fuackons are defined in a such away mop >> ol= 4 Cloo) Value of, object vafrable 2100 Value of class Variable <1 S>> O2= &Ga) Value of object vaw able: ow Value of class variable! yy del Pabhe ond _pivale memben: = Public member can be accessed] ang where nthe \ Program by using : operator Privale members —Privale member of -the class can be accessed on within dhe class asing dok operator. i members a double under - 4 TL cannot be accessed oulstde of class and every Class member bas double underscore a: a prec for Privale omédmber class A: } def __taik~ Cele vi, yay © Self. Vanl=¥) Sel. ——Vagias ya def display Cseig): | | thy oldies ff Print (Wart :¥, sete Yan) PROC Avan: cath, — vers) ag pe ee * dts play © ale : ey ane PTAC Pubke Member "4, ob.vari) yee #20 print (4 Private Menaber :1 obe——Var2) eakpab Vert =16 Vasi=26 Pablic Member S10 . eter peed prik Ceubltc Member’, cb- vari) ota —+ pral(a Privale Member:*, ce a output. Vaz!) Var4 =10 Var2 = 30 Public Member 110 Pavake. Member20 Pivale_rmetbeds Beer salnall are privale mmemben 0°} the clan g Th can be accessed within che clas bal nok | oulside the class “ate double underscore before ‘fanchon mame Tl we want fo access privale imetheds of Clay, Hes we can acen Ah presence of Class by writing % he object name . — class narne ~~ privale me thed (7 Bai ~ta: class A: X defs_imik-- Cself viva) : Self. var) =¥! Self. -- Vav2 = V2 def _- disploy (self): prot ( Varl:", Self. van) print (* Vaia~', self. --Vvar2) ob= A (10,20) ob-- A_-dtsplay¢) print Ce publee Members”, ob vail) print Privale Member :4, ob. a -— Vara) outtake Var 110 Varo! 20 Public Member: Ib Prvake Member:r0 Gallings.class method —fery anethe, clasp rele! s class At def_-iwik-- Gelf, vi) 2 Self. varl = VI i def display (sete) + » promt ce Vail: see. Van) def Mal -to Cseigy ; p Self. varl &=10 Self. — - cfisplay 0) ob= Acie) ab-mul- 1009 cakes Joo while “‘@ class “tofind area of rectangle. class & * de€ -- wik-- (self, 2,8): seit. Vord =L ~ def -- areaCsel ): seth Vara=b prin: Beit Ae sAg-bsl Gx Self-Vvar4 # Self-Var2 print(s Area ob reclandle :", a) ob= A Cto, 20) ob. yp ateac) | | | | ! -202) | ble to eclas Oak: 27-98 5 | Program to add vane fe et | | at rankine. Class Ct def __anik-- Cself, val) * Seif.v= yal del display (sett) « pint (kvas value 13% self.v) —————“‘éaWPD"” éi'tl ob=cClo) q ob-dfsplay 0) algal t—> ab-var=20 outpa Var value ts 10 PAOEC New Yal% ob. voy ‘ar Va output Var value Fs lo New Val +20 ob-var=30 Prink(*New val :# lp 0b: Va) outpal: Var Value 7s to New val: 20 New val * 30 del ob- var Print (* nlew! val 2% ob.ve9 output: 21 oy Built in funclios +40 check, elssek and delele class attributes Bull in-fumelfon: are wed “works albtbibe th | clan. this ut] check whether allfikude are present Io the class and get the Yalwe of atbibule, and 955190 anew value 4o4he athibule and delele the athibuley fon the class | patil ak velams “tue if albtbule present Tnthe class, veturrs false TE thi bule nok presen eit gett attr (obj van Via zt rebans the value of altibute y ppsctatlCobs. Yar, vat): to the athibule In the fo assign a new ‘value class wy detater lob,‘ var'} delele the attibule —from he clas g. class Ct def-- init-- (self, val’) : Seif.v=¥a | def display Cself): print (* Var value %s%, Self-v) ob=cCic) ob- afsployc) print hasalk (ob, \v')) pintChas attr (ob. ‘val')) pink (aver value:’, getattr(ob, Ww") setathr(ob,\V, 45) Print Cab-v) delably (ob, vw) We pinks uclac:* acl oll Cob wv) ‘ El Output ’ Var value Ts to Tue False Vax value: lo aes etree 4. ce hax no fattibule y “Garbage collection” ~ arbage CollecKon 9s automatic shat destroys wthe object. Tt delely all the object at moy be bull type or user defined stupe automaleally when that ate ne longer needed, prog ran Touhy, = | Th flee use, dhe memory space, “ER S>>var=10 per Var to >ss del. var >>? Var ‘error >» Vard=10 pyr Varh= Var 4 oy del Var. dre Varn lo Fag ta Hest ater bates git ge dik 9 ahis alhibule gives a diclfonary Conkaining the classe: or objecls Fe: class ct def __foil __ (seit 1YalA, vak 2) « Self-vi = val. Self Vo = val a. def displey (set) priad (ard value 1s") Self .v,) grial (Muay value is’, celvey ob= CClo, 20) ob- dtsplay C9 paiok Cm object -- drek -—", ob. =-dick--) wZdoc ~- adit Var 4 Value ts 10 Var value Ts 20 7 objet dtel-- 4vi' 10, v2" 204 S'cless c+ “as class Shows class attibule © ** def “mit -- (self, Val4, val >) : Self. vl = Val 4 Selfsv2=> Valo deg display Cif prink (award value Ts, Self.) pial Evaro. value is", self-v2) aba C1020) ob- display ©) petak(s objeck _diek__', ob. — -dick --) prink (¥ objeck --doc--!, ob — - doca~9 prin (a objec —name--%, G --name--) print (* object —~ medale-_4 obs. -modale 2) print ( ou Vaid Value Ts 10 Yar value Ts 30 objeck —- dick __ (y's 10,'Va's 204 object ~- dec ~— This class chows class athibyley object --Name--c object -— module ~~ -- majn~- object -- base -- . oo

You might also like