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

OOPs Concepts

Complete understanding of opps concept.

Uploaded by

rakshit163gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
19 views

OOPs Concepts

Complete understanding of opps concept.

Uploaded by

rakshit163gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 13
DECORATORS A decorator is a way to add or modify the behavior of an object, without changing its inal code. Using a decorator in Python involves defining a decorator function, which takes another function as input, adds the desired functionality, and then returns the modified function. The decorator function can then be applied to any other function, using the "@” symbol before the decorator function name. def test(): print("This is the start of my function") print("This is my function to test") print(4+5) print("This is the end of ny function") test() This is the start of my functior This is my function to test 9 This is the end of my function def decorate (function def inner_decorate(): print("This is the start of my function") function() print("This is the end of my function") return inner_decorate def testi(): print (6+7) test1() 13 @decorate def test1() print(6+7) test1() This is the start of my functior 13 This is the end of my function import time def timer_test(func) : def tiner_test_inner(): start = time.time() fune() end = time.tine() print(end -start) return timer_test_inner def test2(): print (45+78) test2() 123 Gtimer_test def test2(): print (45478) test2() 123 5.1021575927734375e-05 @tiner_test def test3(): for i in range(10@0008000) : pass test3() 21.55422019958496 CLASS METHODS. Class methods are useful when you want to perform some action that invalvas the class as whole, rather than any individual object of the class. To define a class method in Python, you use the ‘@classmethod' decorator before the method definition, Inside the class method, you can access class-level variables and methods using the ‘cls’ parameter, which Fefers to the class itselt. class pwskills : def _init_(self, name, email) : self.name = name self.enail = enail def students_details(self): print(self.name, self.enail) pw = pwskills("Anjali" , "[email protected]") pw.name *anjali pw.email *anjali@gnail .com pw. students_details() Anjali [email protected] class pwskillst : def _init_(self, name, email) : self.name = nane self.enail = email @classmethod def details(cls , name, email): return cls(nane,email) def students_details(self): print(self.name, self.email) Pu = pwskills1.details("Anju", "[email protected]”) pw.name *anju pw1email *[email protected] pwi.students_details() Anju [email protected] class pwskills2 : mobile_number = 9786168618 def _init_(self, name, email) : self.name = nane self.enail = email @classmethod def change_nunber(cls, mobile): pwskills2.mobile_nunber = mobile @classmethod def details(cls , name, email): return cls(nane, email) def students_details(self): print(self.nane, se1f.email,pwskills2.mobile number) puskills2.mobile_nunber 9786168618 puskills2.change_nunber (7893567834) puskills2.mobile_nunber 7893567834 pu_obj = pwski11s2("anju”,"[email protected]") pu_obj.students_details() anju [email protected] 9786168618 pw = pwskills2.details( "Rohan", "Rohanggnail.con") pu. students_details() Rohan [email protected] 9786168618 class pwskills3 mobile_number = 9786168618 def _init_(self, name, email) : self.name = nane self.enail = enail @classmethod def change_nunber(cls, mobile): puskills2.mobile_nunber = mobile @classmethod def details(cls , name, enail): return cls(nane,email) def students_details(self): print(self.nane, self. email, pwskills2.mobile_nunber) def course_details(cls,course_name) : print("Course Name is",course_name) puskills3.course_details = classmethod(course_details) puskills3.course_details("bata Science Masters") Course Name is Data Science Masters def mentor(cls, 1ist_of mentor): print(1ist_of mentor) puskills3.mentor = classmethod(mentor) puskills3.mentor(["Sudhanshu", "krish"]) [‘Sudhanshu', 'krish*] class pwskillsa : mobile_number = 9786168618 def _init_(self, name, email) : self.name = name self.enail = email @classmethod def change_nunber(cls, mobile): pwskills2.mobile_nunber = mobile @classmethod def details(cls , name, email): return cls(nane,email) def students_details(self): print(self.name, self. email, pwskills2.mobile_nunber) del puskills4.change_nunber puskills4.change_nunber (9752189467) Attributerror Traceback (most recent call last) Cell In[59], line 1 => 1 pwskills4.change_number(9752189467) AttributeError: type object 'pwskills4' has no attribute ‘change_nunber’ delattr(puskills4 , "“details") delattr(pwskills4 , "students_details") delattr(pwskills4 , "nobile_nunber") STATIC METHOD A static method is a method that belongs to a class, but does not depend on the state of any object of that class, This means that you can call a static method on the class itself, rather than on an individual object of the class. To define a static method in Python, you use the ‘@staticmethod’ decorator before the method definition. class pwskills : def student_details(self , name , mail_id , number) : print(name , mail_id , nunber) Pw = pwskills() pu. student_details(“Anjali","[email protected]™, 638765783) Anjali [email protected] 638765783 class pwskillsa : def student_details(self , name , mail_id , number) : print(name , mail_id , nunber) @staticnethod def mentor_class(list_mentor) : print (ist_mentor) def mentor(self , mentor_list): print(mentor_list) puskills1.mentor_class(["Sudhanshu", "krish"]) ['Sudhanshu', 'Krish"] student puskills1() student2 = pwskills1() student3 = puskills1() student1mentor(["Sudhanshu", "Krish"]) ('sudhanshu', *krish*] class pwskills2 : def student_details(self , name , mail_id , number) print(name , mail_id , nunber) @staticnethod def mentor_mail_id(mail_id mentor) = print(wail_id_mentor) @staticnethod def mentor_class(1ist_mentor) : puskills2.mentor_mail_id(["[email protected]” ,"[email protected]"]) print(List_mentor) @classmethod def class_nane(cls): cls.mentor_class(["Sudhanshu" ,"krish"]) def mentor(self , mentor_list): print(mentor_list) self.mentor_class(["Sudhanshu" , "krish" ]) pwskills2.class_nane() ['Sudhanshu', '"krish*] puskills2.mentor_class(["Sudhanshu", "krish"]) ['[email protected]’, ‘[email protected]’ [*Sudhanshu', 'krish"] = pwskil1s2() pu.mentor(["Sudhanshu" , "Krish" }) [‘Sudhanshu", 'krish*] [‘[email protected]’, ‘[email protected]’ [*Sudhanshu', 'krish"] ‘SPECIAL (MAGIC/DUNDER) METHODS SPECIAL (MAGIC/DUNDER) METHODS: Special methods (also called magic or dunder methods) are methods used to define how objects of a class behave in various contexts, such as how they are created, compared, printed, or used in mathematical operations. Special methods are recognizable 3 their double underscore prefix and suffix, such as “str_',"_eq_', and so on. dir(int) _floordiv “Format_*, —ee_'; getattribute_', _rpow_", _rrshift, Crshitt_", ‘Trsub_7, Crtruediv_', rxor_', Csetattr. zeof_", _str_', sub", "—subclasshook_', xor_", ‘as_integer_ratio’ bit_count” bit_length’, conjugate’, denominator’ , fron_bytes", imag’, numerator, ‘real’, to_bytes'] dir(str) ['_add_* Telass_', ‘contains, Taelattr_—", Tdir_', doc", ‘eq_", “Format_', ‘—ge_', “getattribute_' Tgetiten_', Tinit_', init subclass_', Titer_' te) Ten’, Tat, Tod} mul", "ne, Chew", reduce", —reduce_ex_ _repr_' —rmod_*; Crm", _setattr Csizeot_", str" 'Tsubelasshook_', capitalize’, casefold', ‘center’, ‘count’, encode’, endswith’, expandtabs’ , ‘Find’, “Format, format_map", index", isalnun’, isalpha’, isascii', isdecimal', isdigit', isidentifier', islower', isnumeric’, isprintable', ‘isspace’, istitle’, supper’, ‘join’, ‘just’, lower", ‘Istrip', ‘maketrans*, partition’, removeprefix' , removesuffix', ‘replace’, tind’ ‘rindex' , ‘rjust", ‘rpartition’, ‘rsplit’, ‘rstrip’, ‘split’, splitLines’, startswith’, ‘strip’, "swapcase’ ‘title’, ‘translate’, ‘upper’, 2fill'] a= 100 ass i]: 105 a._add_(5) 105 class pwskillss def _new_(cls): print("This Is My New") def _: (self): print("This Is My INIT") self.mobile_nunber = 7383838383 pw = pwskillss() This Is My New pu.mobile_nunber 7383838383 class pwskills6 : def _ini (self): self.mobile_number = 7383838383 def _str_(self) Feturn "This Is My Magic Call Of str pan = pwskil1s6() pw <_main__.pwskillsé at @x7f941d58a0e0> print(pw1) This Is My Magic Call OF str PROPERTY DECORATORS Property decorators are used to define properties. A property is defined by using the (property decorator before the getter method, and optionally using the @propertyname.setter' and "@propertyname.deleter decorators before the setter and deleter methods, respectively. GETTERS A getter is a method that is used to get the value of an attribute of an object, Getters are used to encapsulate attributes and control their access, so that other parts of the program cantt access or modify them directly. SETTERS A setter isa method that is used to set the value of an attribute of an object. Setters are Used to encapsulate attributes and control their modification, so that other parts of the program cant modify them directly. DELETERS A deleter is a method that is used to delete or remove an attribute of an object. Deleters are typically used to control the deletion of attributes and ensure that any necessary clean-up class pwskills7 : def _in’ (self , course_price, course_name): self.__course_price = course_price self.course_nane = course_name @property def course_price_access(self): return self. course _price @course_price_access.setter def course_price_set(self , price ): if price <= 3500: pass else: self.__course_price = price @course_price_access.deleter def delete_course_price(self): del self.__course_price Pw = pwskills7(350, “Data Science Masters") pw.course_price AtteibuteError Cell In[1381, line 1 > 1 pw.course_price Traceback (most recent call last) AttributeError: 'pwskills7' object has no attribute ‘course_price pu._pwskills7_course_price 3500 pu. course_nane “Data Science Masters pw.course_price_access 3500 pu.course_price_set = 450 pw.course_price_access 4500 pu.course_price_set = 3000 pw.course_price_access 3500 pu.course_price_access 3500 del pu.delete_course_price pu.course_price_access Attributetrror Traceback (most recent call last) Cell In[161], line 1 -> 1 pw.course_price_access Cell In[157], line 10, in pwskills7.course_price_access(self) 8 Gproperty 9 def course_price access(self) <--> 1 return self. _course_price AttributeError: 'pwskills7' object has no attribute ‘_pwskills7_course_price

You might also like