{"id":465,"date":"2020-10-11T12:06:13","date_gmt":"2020-10-11T12:06:13","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=465"},"modified":"2025-03-31T07:56:59","modified_gmt":"2025-03-31T07:56:59","slug":"python-class-methods","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-class-methods\/","title":{"rendered":"Python Class Methods"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python class methods and when to use them appropriately.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-class-methods'>Introduction to Python class methods <a href=\"#introduction-to-python-class-methods\" class=\"anchor\" id=\"introduction-to-python-class-methods\" title=\"Anchor for Introduction to Python class methods\">#<\/a><\/h2>\n\n\n\n<p>So far, you learned about <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-methods\/\">instance methods<\/a> that are bound to a specific instance of a <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a>.<\/p>\n\n\n\n<p>Instance methods can access <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-instance-variables\/\">instance variables<\/a> within the same class. To invoke instance methods, you need to create an instance of the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a> first.<\/p>\n\n\n\n<p>The following defines the <code>Person<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, first_name, last_name, age)<\/span>:<\/span>\n        self.first_name = first_name\n        self.last_name = last_name\n        self.age = age\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_full_name<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{self.first_name}<\/span> <span class=\"hljs-subst\">{self.last_name}<\/span>\"<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">introduce<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Hi. I'm <span class=\"hljs-subst\">{self.first_name}<\/span> <span class=\"hljs-subst\">{self.last_name}<\/span>. I'm <span class=\"hljs-subst\">{self.age}<\/span> years old.\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Person<\/code> class has three instance methods including <code><a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-__init__\/\">__init__()<\/a><\/code>, <code>get_full_name()<\/code>, and <code>introduce()<\/code>.<\/p>\n\n\n\n<p>Suppose that you want to add a method that creates an anonymous person to the <code>Person<\/code> class. <\/p>\n\n\n\n<p>In order to do so, you would come up with the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-comment\"># ... other methods<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">create_anonymous<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> Person(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-number\">25<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>create_anonymous()<\/code> is an instance method that returns an anonymous person. <\/p>\n\n\n\n<p>However, to invoke the <code>create_anonymous()<\/code> method, you need to create an instance, which doesn&#8217;t make sense in this case.<\/p>\n\n\n\n<p>This is why Python class methods come into play.<\/p>\n\n\n\n<p>A class method isn&#8217;t bound to any specific instance. It&#8217;s bound to the class only.<\/p>\n\n\n\n<p>To define a class method:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First place the <code>@classmethod<\/code> <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-decorators\/\">decorator<\/a> above the method definition. For now, you just need to understand that the <code>@classmethod<\/code> decorator will change an instance method to a class method.<\/li>\n\n\n\n<li>Second, rename the <code>self<\/code> parameter to <code>cls<\/code>. The <code>cls<\/code> means <code>class<\/code>. However, <code>class<\/code> is a keyword so you cannot use it as a parameter.<\/li>\n<\/ul>\n\n\n\n<p>The following shows the new version of the <code>Person<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Person<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, first_name, last_name, age)<\/span>:<\/span>\n        self.first_name = first_name\n        self.last_name = last_name\n        self.age = age\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_full_name<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"<span class=\"hljs-subst\">{self.first_name}<\/span> <span class=\"hljs-subst\">{self.last_name}<\/span>\"<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">introduce<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Hi. I'm <span class=\"hljs-subst\">{self.first_name}<\/span> <span class=\"hljs-subst\">{self.last_name}<\/span>. I'm <span class=\"hljs-subst\">{self.age}<\/span> years old.\"<\/span>\n\n<span class=\"hljs-meta\">    @classmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">create_anonymous<\/span><span class=\"hljs-params\">(cls)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> Person(<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-number\">25<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>create_anonymous()<\/code> method cannot access instance attributes. But it can access <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-attributes\/\">class attributes<\/a> via the <code>cls<\/code> variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='calling-python-class-methods'>Calling Python class methods <a href=\"#calling-python-class-methods\" class=\"anchor\" id=\"calling-python-class-methods\" title=\"Anchor for Calling Python class methods\">#<\/a><\/h2>\n\n\n\n<p>To call a class method, you use the class name, followed by a dot, and then the method name like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">ClassName.method_name()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example shows how to call the <code>create_anonymous()<\/code> class method of the Person class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">anonymous = Person.create_anonymous()\nprint(anonymous.introduce())\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Hi. I<span class=\"hljs-string\">'m John Doe. I'<\/span>m <span class=\"hljs-number\">25<\/span> years old.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='class-methods-vs-instance-methods'>Class methods vs. instance methods <a href=\"#class-methods-vs-instance-methods\" class=\"anchor\" id=\"class-methods-vs-instance-methods\" title=\"Anchor for Class methods vs. instance methods\">#<\/a><\/h2>\n\n\n\n<p>The following table illustrates the differences between class methods and instance methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Features<\/strong><\/th><th><strong>class methods<\/strong><\/th><th><strong>Instance methods<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Binding<\/td><td>Class<\/td><td>An instance of the class<\/td><\/tr><tr><td>Calling<\/td><td><code>Class.method()<\/code><\/td><td><code>object.method()<\/code><\/td><\/tr><tr><td>Accessing<\/td><td>Class attributes<\/td><td>Instance &amp; class attributes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='when-to-use-python-class-methods'>When to use Python class methods <a href=\"#when-to-use-python-class-methods\" class=\"anchor\" id=\"when-to-use-python-class-methods\" title=\"Anchor for When to use Python class methods\">#<\/a><\/h2>\n\n\n\n<p>You can use class methods for any methods that are not bound to a specific instance but the class. In practice, you often use class methods for methods that create an instance of the class. <\/p>\n\n\n\n<p>When a method creates an instance of the class and returns it, the method is called a <strong>factory method<\/strong>. For example, the <code>create_anonymous()<\/code> is a factory method because it returns a new instance of the <code>Person<\/code> class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python class methods aren&#8217;t bound to any specific instance, but classes.<\/li>\n\n\n\n<li>Use <code>@classmethod<\/code> decorator to change an instance method to a class method. Also, pass the <code>cls<\/code> as the first parameter to the class method.<\/li>\n\n\n\n<li>Use class methods for factory methods. <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=class-method\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"465\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-methods\/\"\n\t\t\t\tdata-post-title=\"Python Class Methods\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"465\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-methods\/\"\n\t\t\t\tdata-post-title=\"Python Class Methods\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about Python class methods and when to use them appropriately.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-465","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=465"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/465\/revisions"}],"predecessor-version":[{"id":7297,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/465\/revisions\/7297"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/417"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}