{"id":77880,"date":"2020-04-03T12:53:36","date_gmt":"2020-04-03T07:23:36","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=77880"},"modified":"2024-08-22T18:07:14","modified_gmt":"2024-08-22T12:37:14","slug":"python-methods","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-methods\/","title":{"rendered":"Python Methods &#8211; Learn to Create &amp; Add a Method to Class"},"content":{"rendered":"<p>As we all have learned by now, <strong>Python<\/strong> supports <strong>object-oriented programming<\/strong>. That basically means Python programs can have <strong>classes<\/strong> and <strong>objects<\/strong>.<\/p>\n<p>Moreover, <strong>classes<\/strong> let you <strong>bundle behavior<\/strong> and <strong>state together<\/strong>. This is where <strong>methods<\/strong> come into the <strong>picture<\/strong>.<\/p>\n<p><strong>Methods<\/strong> are a way to <strong>implement<\/strong> and <strong>define<\/strong> a <strong>class\u2019s behavior<\/strong>. So methods can be thought of as <strong>functions <\/strong>defined <strong>within a class<\/strong>. They allow objects to perform actions and interact with each other, enabling the creation of complex, modular, and reusable code. Additionally, methods can access and modify the data encapsulated within an object, ensuring that the internal state remains consistent and controlled.<\/p>\n<h3>Python Methods<\/h3>\n<p>A method in <strong>object-oriented programming<\/strong> is a procedure that represents the <strong>behavior<\/strong> of a <strong>class<\/strong> and is associated with the <strong>objects<\/strong> of that <strong>class<\/strong>. These methods define the actions that an object created from the class can perform and can manipulate the object&#8217;s data.<\/p>\n<p>Let\u2019s understand the <strong>working of methods<\/strong> at a <strong>greater depth<\/strong>.<\/p>\n<p>But before that, we need to <strong>create a class<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Display:\r\n\u00a0\u00a0\u00a0\u00a0pass<\/pre>\n<p><strong>Let\u2019s break this code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Display:\r\n<\/pre>\n<ul>\n<li>The definition of a class starts with the <strong>class keyword<\/strong>.<\/li>\n<li><strong>Display<\/strong> is the name of the class.<\/li>\n<li>The <strong>colon ( : )<\/strong> signifies the beginning of a suite.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">pass\r\n<\/pre>\n<ul>\n<li>The <strong>pass keyword<\/strong> is used when you want to <strong>define<\/strong> a <strong>class<\/strong> but you do not yet know <strong>what to put inside<\/strong> that <strong>class.<\/strong><\/li>\n<\/ul>\n<p>The class looks <strong>empty<\/strong> right? Well, let\u2019s add some <strong>behavior<\/strong> to this class.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/how-to-add-python-methods-to-a-class.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77945\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/how-to-add-python-methods-to-a-class.jpg\" alt=\"\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<h3>Adding Python Methods to a class<\/h3>\n<p>Let\u2019s <strong>remove<\/strong> the <strong>pass keyword<\/strong> and <strong>add<\/strong> a <strong>method<\/strong> to our <strong>Display class.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Display:\r\n\u00a0\u00a0\u00a0\u00a0def show(self):\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0print(\"Hi from Techvidvan!\")<\/pre>\n<p>This entire thing which is written inside the <strong>class suite<\/strong> is a <strong>method.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def show(self):\r\n<\/pre>\n<ul>\n<li>A method, much like a <strong>normal function,<\/strong> starts with a <strong>def keyword.<\/strong><\/li>\n<li><strong>show<\/strong> is the <strong>name<\/strong> of our <strong>method.<\/strong><\/li>\n<li>It takes a <strong>single argument self<\/strong>, written <strong>inside<\/strong> the <strong>parenthesis<\/strong>.<\/li>\n<li>The <strong>colon ( : )<\/strong> specifies the <strong>start of a suite<\/strong>.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print(\"Hi from Techvidvan!\")<\/pre>\n<ul>\n<li>This is the statement written inside the method\u2019s suite.<\/li>\n<li>It prints the string written inside the parenthesis.<\/li>\n<\/ul>\n<p>That was about <strong>adding<\/strong> a <strong>python method<\/strong>.<\/p>\n<p>Now let\u2019s see how to <strong>create objects<\/strong> and <strong>use <\/strong>this method.<\/p>\n<h3>Invoking Python Method<\/h3>\n<p>To invoke a method, we first need to <strong>create an object<\/strong> of the <strong>class<\/strong>. We can then <strong>invoke<\/strong> the method on the <strong>newly created<\/strong> <strong>object.<\/strong><\/p>\n<p>Go to the <strong>Python shell<\/strong> and type the following:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; d = Display()<\/pre>\n<p>This <strong>creates<\/strong> an <strong>object d<\/strong> of <strong>class Display<\/strong>.<\/p>\n<p>Now, invoke the method <strong>show()<\/strong> on the <strong>object d<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; d.show()<\/pre>\n<p>We call a <strong>method on an object<\/strong> using the <strong>dot notation<\/strong>.<\/p>\n<p><strong>The output is as follows:<\/strong><\/p>\n<div class=\"code-output\">&gt;&gt;&gt; d = Display()<br \/>\n&gt;&gt;&gt; d.show()<br \/>\nHi from Techvidvan!<br \/>\n&gt;&gt;&gt;<\/div>\n<p>Notice that while defining the method we passed an <strong>argument self<\/strong>, but while <strong>calling <\/strong>the <strong>method<\/strong> we <strong>didn\u2019t pass<\/strong> any <strong>value<\/strong> to that <strong>argument<\/strong>. This brings us to our next section, that is, the <strong>significance of self<\/strong>.<\/p>\n<h3>The significance of \u201cself\u201d<\/h3>\n<p>When we invoke the method as :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">d.show()<\/pre>\n<p>the <strong>interpreter<\/strong> turns this <strong>line of code<\/strong> into something like this :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Display.show(d)<\/pre>\n<p>where,<\/p>\n<p><strong>Display<\/strong> is the <strong>class name<\/strong>, <strong>show<\/strong> is the <strong>method name<\/strong>, and <strong>d<\/strong> is the <strong>object name<\/strong>, passed as an <strong>argument<\/strong> to <strong>show()<\/strong>.<\/p>\n<p>Clearly, the interpreter assigns the value of <strong>\u201cd\u201d<\/strong> to<strong> \u201cself\u201d<\/strong>. This is why every method needs its <strong>first parameter<\/strong> to be <strong>\u201cself\u201d.<\/strong><\/p>\n<p>Now that you\u2019ve understood the importance of <strong>self<\/strong>, let\u2019s <strong>create<\/strong> a <strong>method<\/strong> that does something quite more than just <strong>printing<\/strong> some <strong>string<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class Display:\r\n    def __init__(self, val):\r\n        self.val = val\r\n    def increase(self):\r\n        self.val += 1\r\n    def show(self):\r\n        print(self.val)<\/pre>\n<p>Inside the class, there are three methods namely <strong>__init__()<\/strong>, <strong>increase()<\/strong>,<strong> show()<\/strong>.<\/p>\n<p>The <strong>class<\/strong> also has an <strong>instance variable val<\/strong>.<\/p>\n<ul>\n<li>The <strong>__init__()<\/strong> method is called a <strong>constructor method<\/strong>. It <strong>initializes<\/strong> the <strong>value<\/strong> of all the <strong>instance variables <\/strong>of a<strong> class<\/strong>.<\/li>\n<li>The<strong> increase()<\/strong> method <strong>increments<\/strong> the <strong>value<\/strong> of the <strong>attribute val by 1<\/strong>.<\/li>\n<li>The <strong>show()<\/strong> method <strong>displays<\/strong> the <strong>value<\/strong> of the <strong>attribute val<\/strong>.<\/li>\n<\/ul>\n<p>This is what the <strong>method call outputs<\/strong>:<\/p>\n<div class=\"code-output\">&gt;&gt;&gt; c = Display(100)<br \/>\n&gt;&gt;&gt; c.inscrease()<br \/>\n&gt;&gt;&gt; c.show()<br \/>\n101<br \/>\n&gt;&gt;&gt;<\/div>\n<p>Notice that the <strong>value<\/strong> of the <strong>instance variable val<\/strong> is <strong>passed<\/strong> while <strong>creating<\/strong> the <strong>object c<\/strong>.<\/p>\n<h3>Summary<\/h3>\n<p>So that is how methods work in <strong>python<\/strong>.<\/p>\n<p>We learnt that <strong>methods<\/strong> are a <strong>past of class<\/strong> and are <strong>associated<\/strong> with <strong>objects<\/strong> of that <strong>class<\/strong>. The <strong>data<\/strong> of an <strong>object<\/strong> can be <strong>accessed<\/strong> only by the <strong>methods<\/strong> of that <strong>object<\/strong>. We learnt how to <strong>create<\/strong> and <strong>add<\/strong> a <strong>method <\/strong>to a<strong> class<\/strong>, <strong>how to call<\/strong> that <strong>method<\/strong> by <strong>creating <\/strong>an <strong>object<\/strong>.<\/p>\n<p>We also learnt what the <strong>interpreter reads<\/strong> when you <strong>call a method<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As we all have learned by now, Python supports object-oriented programming. That basically means Python programs can have classes and objects. Moreover, classes let you bundle behavior and state together. This is where methods&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":77945,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2247,2248,2249,2250,1235],"class_list":["post-77880","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-adding-python-method-to-a-class","tag-create-python-method","tag-invoking-python-methods","tag-methods-in-python","tag-python-methods"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Methods - Learn to Create &amp; Add a Method to Class - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Methods - With this article get to know about Python Methods in detail and also learn how to create, invoke and add a python method to a class.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Methods - Learn to Create &amp; Add a Method to Class - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Methods - With this article get to know about Python Methods in detail and also learn how to create, invoke and add a python method to a class.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-03T07:23:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-22T12:37:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/how-to-add-python-methods-to-a-class.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Methods - Learn to Create &amp; Add a Method to Class - TechVidvan","description":"Python Methods - With this article get to know about Python Methods in detail and also learn how to create, invoke and add a python method to a class.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-methods\/","og_locale":"en_US","og_type":"article","og_title":"Python Methods - Learn to Create &amp; Add a Method to Class - TechVidvan","og_description":"Python Methods - With this article get to know about Python Methods in detail and also learn how to create, invoke and add a python method to a class.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-methods\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-03T07:23:36+00:00","article_modified_time":"2024-08-22T12:37:14+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/how-to-add-python-methods-to-a-class.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Methods &#8211; Learn to Create &amp; Add a Method to Class","datePublished":"2020-04-03T07:23:36+00:00","dateModified":"2024-08-22T12:37:14+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/"},"wordCount":746,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/how-to-add-python-methods-to-a-class.jpg","keywords":["adding python method to a class","create python method","Invoking python methods","methods in python","python methods"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-methods\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/","url":"https:\/\/techvidvan.com\/tutorials\/python-methods\/","name":"Python Methods - Learn to Create &amp; Add a Method to Class - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/how-to-add-python-methods-to-a-class.jpg","datePublished":"2020-04-03T07:23:36+00:00","dateModified":"2024-08-22T12:37:14+00:00","description":"Python Methods - With this article get to know about Python Methods in detail and also learn how to create, invoke and add a python method to a class.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-methods\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/how-to-add-python-methods-to-a-class.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/how-to-add-python-methods-to-a-class.jpg","width":802,"height":420,"caption":"add python methods to a class"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Methods &#8211; Learn to Create &amp; Add a Method to Class"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=77880"}],"version-history":[{"count":1,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77880\/revisions"}],"predecessor-version":[{"id":447681,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77880\/revisions\/447681"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/77945"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}