What is difference between self and __init__ methods ...
文章推薦指數: 80 %
"__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
延伸文章資訊
- 1Python中__init__和self的意义和作用 - CSDN博客
以学生类为例,通过定义一个特殊的__init__方法,在创建实例的时候,就把name,score等属性绑上去:class Student(object):def __init__(self, n...
- 2What do __init__ and self do in Python? - Stack Overflow
Here __init__ acts as a constructor for the class and when an object is instantiated, this functi...
- 3Understanding self and __init__ method in python Class.
"__init__" is a reseved method in python classes. It is known as a constructor in object oriented...
- 4__init__ in Python - GeeksforGeeks
Prerequisites – Python Class, Objects, Self Whenever object-oriented programming is done in Pytho...
- 5What is def __init__ self in Python? - Educative.io
What is def __init__ self in Python? ... Ace your System Design Interview and take your career to...