What is difference between self and __init__ methods ...

文章推薦指數: 80 %
投票人數:10人

"__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an ... Home CodingGround Jobs Whiteboard Tools Business Teachwithus TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Whatisdifferencebetweenselfand__init__methodsinpythonClass? PythonServerSideProgrammingProgramming BeyondBasicProgramming-IntermediatePython MostPopular 36Lectures 3hours MohammadNauman MoreDetail PracticalMachineLearningusingPython BestSeller 91Lectures 23.5hours MANASDASGUPTA MoreDetail PracticalDataScienceusingPython 22Lectures 6hours MANASDASGUPTA MoreDetail selfTheword'self'isusedtorepresenttheinstanceofaclass.Byusingthe"self"keywordweaccesstheattributesandmethodsoftheclassinpython.__init__method"__init__"isaresevedmethodinpythonclasses.Itiscalledasaconstructorinobjectorientedterminology.Thismethodiscalledwhenanobjectiscreatedfromaclassanditallowstheclasstoinitializetheattributesoftheclass.ExampleFindoutthecostofarectangularfieldwithbreadth(b=120),length(l=160).Itcostsx(2000)rupeesper1squareunitclassRectangle:   def__init__(self,length,breadth,unit_cost=0):     self.length=length     self.breadth=breadth     self.unit_cost=unit_cost   defget_area(self):     returnself.length*self.breadth   defcalculate_cost(self):     area=self.get_area()     returnarea*self.unit_cost #breadth=120units,length=160units,1squnitcost=Rs2000 r=Rectangle(160,120,2000) print("AreaofRectangle:%ssqunits"%(r.get_area()))OutputThisgivestheoutputAreaofRectangle:19200squnits Costofrectangularfield:Rs.38400000 RajendraDharmkar Updatedon13-Jun-202008:30:30 RelatedQuestions&AnswersselfinPythonclass Differencebetweentitle()andwm_title()methodsinTkinterclass Whatisthedifferencebetweenre.match(),re.search()andre.findall()methodsinPython? Whatisthedifferencebetweenre.findall()andre.finditer()methodsavailableinPython? Whatisthedifferencebetweenscipy.cluster.vq.kmeans()andscipy.cluster.vq.kmeans2()methods? WhatisthedifferencebetweenfunctionsandmethodsinJavaScript? Whatisthedifferencebetweennon-staticmethodsandabstractmethodsinJava? Whatisthedifferencebetweenre.search()andre.findall()methodsinPythonregularexpressions? WhatisthedifferencebetweenString.Copy()andString.Clone()methodsinC#? WhatisthedifferencebetweenString.Copy()andString.CopyTo()methodsinC#? WhatisthedifferencebetweenWrite()andWriteLine()methodsinC#? WhatisthedifferencebetweenTrim()andTrimStart()methodsinC#? WhatisthedifferencebetweenRead()andReadLine()methodsinC#? WhatisthedifferencebetweenswitchClass()andtoggleClass()methodsinjQuery? Whatisthedifferencebetween$.closest()and$.parents()methodsinjQuery? PreviousPage PrintPage NextPage  Advertisements



請為這篇文章評分?