{"id":12244,"date":"2021-02-02T08:03:14","date_gmt":"2021-02-02T08:03:14","guid":{"rendered":"https:\/\/www.askpython.com\/?p=12244"},"modified":"2021-02-22T16:16:23","modified_gmt":"2021-02-22T16:16:23","slug":"python-classmethod","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/oops\/python-classmethod","title":{"rendered":"Python classmethod()"},"content":{"rendered":"\n<p>Python classmethod() is a built-in function of the Python standard library!<\/p>\n\n\n\n<p>There are <strong>three types of methods<\/strong> in Python:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Instance Method<\/li><li><strong>Class Method<\/strong><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/staticmethod-in-python\" class=\"rank-math-link\">Static Method<\/a><\/li><\/ul>\n\n\n\n<p>Here, in this article, we&#8217;re going to discuss and focus on the class method of Python. So let&#8217;s get started.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Python classmethod()?<\/h2>\n\n\n\n<p>A <strong>Python classmethod()<\/strong> can be called by a class as well as by an object. A method that returns a class method for a given function is called a classmethod(). <\/p>\n\n\n\n<p>It is a method that is shared among all objects. It is an in-built function in the Python programming language. <\/p>\n\n\n\n<p>A classmethod() takes only one single parameter called function. Hence, the syntax of the classmethod() method in python is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSyntax : classmethod(function)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Using the Python classmethod() <\/h2>\n\n\n\n<p>Let&#8217;s take a look at an example of the Python classmethod() function here. In the below example, we use the classmethod() function to print the marks of the student that are declared within the class instead of passing the variable to the function. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Student:\n    marks = 50\n\n    def printMarks(cls):\n        print(&quot;Marks obtained by the Student is:&quot;,cls.marks)\n\nStudent.printMarks=classmethod(Student.printMarks)\nStudent.printMarks()\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n============= RESTART: C:\/Users\/Admin\/Student.py =============\nMarks obtained by the Student is: 50\n<\/pre><\/div>\n\n\n<p><strong>In the above code, we observe that:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We have a class Student with member variable <strong>marks<\/strong>. <\/li><li>We have a function <strong>printMarks<\/strong> which accepts parameter <strong>cls<\/strong>. <\/li><li>We have passed the classmethod() that is always attached to a class. <\/li><li>Here, the first argument is always a class itself which is why we use &#8216;<strong>cls&#8217; <\/strong>when we define the class. <\/li><li>Then we pass the method\u00a0<strong>Student.printMarks\u00a0<\/strong>as an argument to<strong> classmethod( )<\/strong>. The finally <strong>printMarks<\/strong> is called\u00a0that prints the class variable\u00a0<strong>marks<\/strong>.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Decorator @classmethod<\/h2>\n\n\n\n<p>Another method to use the classmethod( ) function is by using the <strong>@classmethod Decorator.<\/strong> <\/p>\n\n\n\n<p>Instead of the object, by using the decorator method we can call the class name. It can be applied to any method of the class.<\/p>\n\n\n\n<p>The syntax of the <strong>@classmethod Decorator<\/strong> method is as follows: It takes one parameter and multiple arguments.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass c(object):\n    @classmethod\n    def function(cls, argument1, argument2, ...):\n    \/*remaining code*\/\n<\/pre><\/div>\n\n\n<p>Let&#8217;s see how we can run a similar example with the use of the decoartor classmethod instead of using the function as in the previous example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Student:\n    course = &#039;Python&#039;\n\n    @classmethod\n    def printCourse(cls):\n        print(&quot;Course opted by the Student is:&quot;,cls.course)\n\nStudent.printCourse()\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n============= RESTART: C:\/Users\/Admin\/Student.py =============\nCourse opted by the Student is: Python\n<\/pre><\/div>\n\n\n<p>In the example mentioned above, the <strong>@classmethod decorator<\/strong> has applied to the <strong>printCourse( )<\/strong> method. It has one parameter <strong>cls<\/strong>. The method then automatically picks the member variable of the class instead of requiring the user to pass one to the function. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This was in brief about the two ways in which you can use the Python classmethod( ) function. Stay tuned for more articles on Python! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python classmethod() is a built-in function of the Python standard library! There are three types of methods in Python: Instance Method Class Method Static Method Here, in this article, we&#8217;re going to discuss and focus on the class method of Python. So let&#8217;s get started. What is a Python classmethod()? A Python classmethod() can be [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":12687,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-12244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oops"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/12244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=12244"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/12244\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/12687"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=12244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=12244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=12244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}