{"id":440,"date":"2020-10-10T14:00:01","date_gmt":"2020-10-10T14:00:01","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=440"},"modified":"2020-11-13T06:39:10","modified_gmt":"2020-11-13T06:39:10","slug":"python-class-attributes","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-oop\/python-class-attributes\/","title":{"rendered":"Python Class Attributes"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python class attributes and when to use them appropriately.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-class-attributes'>Introduction to class attributes <a href=\"#introduction-to-class-attributes\" class=\"anchor\" id=\"introduction-to-class-attributes\" title=\"Anchor for Introduction to class attributes\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s start with a simple <code>Circle<\/code> <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">class<\/a>:<\/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\">Circle<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, radius)<\/span>:<\/span>\n        self.pi = <span class=\"hljs-number\">3.14159<\/span>\n        self.radius = radius\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.pi * self.radius**<span class=\"hljs-number\">2<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">circumference<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">2<\/span>*self.pi * self.radius\n<\/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>Circle<\/code> class has two attributes <code>pi<\/code> and <code>radius<\/code>. It also has two methods that calculate the area and circumference of a circle.<\/p>\n\n\n\n<p>Both <code>pi<\/code> and <code>radius<\/code> are called <strong>instance attributes<\/strong>. In other words, they belong to a specific instance of the <code>Circle<\/code> class. If you change the attributes of an instance, it won&#8217;t affect other instances.<\/p>\n\n\n\n<p>Besides instance attributes, Python also supports <strong>class attributes<\/strong>. The class attributes don&#8217;t associate with any specific instance of the class. But they&#8217;re shared by all instances of the class.<\/p>\n\n\n\n<p class=\"note\">If you&#8217;ve been programming in Java or C#, you&#8217;ll see that class attributes are similar to the static members, but not the same.<\/p>\n\n\n\n<p>To define a class attribute, you place it outside of the <code>__init__()<\/code> method. For example, the following defines <code>pi<\/code> as a class attribute:<\/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\">Circle<\/span>:<\/span>\n    pi = <span class=\"hljs-number\">3.14159<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, radius)<\/span>:<\/span>\n        self.radius = radius\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.pi * self.radius**<span class=\"hljs-number\">2<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">circumference<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">2<\/span> * self.pi * self.radius\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>After that, you can access the class attribute via instances of the class or via the class name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Oracle Rules Language\" data-shcb-language-slug=\"ruleslanguage\"><span><code class=\"hljs language-ruleslanguage\">object_name.class_attribute\nclass_name.class_attribute<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Oracle Rules Language<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">ruleslanguage<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code>area()<\/code> and <code>circumference()<\/code> methods, we access the <code>pi<\/code> class attribute via the <code>self<\/code> variable.<\/p>\n\n\n\n<p>Outside the <code>Circle<\/code> class, you can access the <code>pi<\/code> class attribute via an instance of the <code>Circle<\/code> class or directly via the <code>Circle<\/code> class. For example:<\/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\">c = Circle(<span class=\"hljs-number\">10<\/span>)\nprint(c.pi)\nprint(Circle.pi)<\/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>Output:<\/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\"><span class=\"hljs-number\">3.14159<\/span>\n<span class=\"hljs-number\">3.14159<\/span><\/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<h2 class=\"wp-block-heading\" id='how-python-class-attributes-work'>How Python class attributes work <a href=\"#how-python-class-attributes-work\" class=\"anchor\" id=\"how-python-class-attributes-work\" title=\"Anchor for How Python class attributes work\">#<\/a><\/h2>\n\n\n\n<p>When you access an attribute via an instance of the class, Python searches for the attribute in the instance attribute list. If the instance attribute list doesn&#8217;t have that attribute, Python continues looking up the attribute in the class attribute list. Python returns the value of the attribute as long as it finds the attribute in the instance attribute list or class attribute list.<\/p>\n\n\n\n<p>However, if you access an attribute, Python directly searches for the attribute in the class attribute list. <\/p>\n\n\n\n<p>The following example defines a <code>Test<\/code> class to demonstrate how Python handles instance and class attributes.<\/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\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Test<\/span>:<\/span>\n    x = <span class=\"hljs-number\">10<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.x = <span class=\"hljs-number\">20<\/span>\n\n\ntest = Test()\nprint(test.x)  <span class=\"hljs-comment\"># 20<\/span>\nprint(Test.x)  <span class=\"hljs-comment\"># 10<\/span><\/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<p>How it works.<\/p>\n\n\n\n<p>The <code>Test<\/code> class has two attributes with the same name (<code>x<\/code>) one is the instance attribute and the other is a class attribute.<\/p>\n\n\n\n<p>When we access the <code>x<\/code> attribute via the instance of the <code>Test<\/code> class, it returns 20 which is the variable of the instance attribute.<\/p>\n\n\n\n<p>However, when we access the <code>x<\/code> attribute via the <code>Test<\/code> class, it returns 10 which is the value of the <code>x<\/code> class attribute.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='when-to-use-python-class-attributes'>When to use Python class attributes <a href=\"#when-to-use-python-class-attributes\" class=\"anchor\" id=\"when-to-use-python-class-attributes\" title=\"Anchor for When to use Python class attributes\">#<\/a><\/h2>\n\n\n\n<p>Class attributes are useful in some cases such as storing class constants, tracking data across all instances, and defining default values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-storing-class-constants'>1) Storing class constants <a href=\"#1-storing-class-constants\" class=\"anchor\" id=\"1-storing-class-constants\" title=\"Anchor for 1) Storing class constants\">#<\/a><\/h3>\n\n\n\n<p>Since a constant doesn&#8217;t change from instance to instance of a class, it&#8217;s handy to store it as a class attribute.<\/p>\n\n\n\n<p>For example, the <code>Circle<\/code> class has the <code>pi<\/code> constant that is the same for all instances of the class. Therefore, it&#8217;s a good candidate for the class attributes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-tracking-data-across-of-all-instances'>2) Tracking data across of all instances <a href=\"#2-tracking-data-across-of-all-instances\" class=\"anchor\" id=\"2-tracking-data-across-of-all-instances\" title=\"Anchor for 2) Tracking data across of all instances\">#<\/a><\/h3>\n\n\n\n<p>The following adds the <code>circle_list<\/code> class attribute to the <code>Circle<\/code> class. When you create a new instance of the <code>Circle<\/code> class, the constructor adds the instance to the list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" 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\">Circle<\/span>:<\/span>\n    circle_list = &#91;]\n    pi = <span class=\"hljs-number\">3.14159<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, radius)<\/span>:<\/span>\n        self.radius = radius\n        <span class=\"hljs-comment\"># add the instance to the circle list<\/span>\n        self.circle_list.append(self)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.pi * self.radius**<span class=\"hljs-number\">2<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">circumference<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">2<\/span> * self.pi * self.radius\n\n\nc1 = Circle(<span class=\"hljs-number\">10<\/span>)\nc2 = Circle(<span class=\"hljs-number\">20<\/span>)\n\nprint(len(Circle.circle_list))  <span class=\"hljs-comment\"># 2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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<h3 class=\"wp-block-heading\" id='3-defining-default-values'>3) Defining default values <a href=\"#3-defining-default-values\" class=\"anchor\" id=\"3-defining-default-values\" title=\"Anchor for 3) Defining default values\">#<\/a><\/h3>\n\n\n\n<p>Sometimes, you want to set a default value for all instances of a class. In this case, you can use a class attribute.<\/p>\n\n\n\n<p>The following example defines a <code>Product<\/code> class. All the instances of the <code>Product<\/code> class will have a default discount specified by the <code>default_discount<\/code> class attribute:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" 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\">Product<\/span>:<\/span>\n    default_discount = <span class=\"hljs-number\">0<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, price)<\/span>:<\/span>\n        self.price = price\n        self.discount = Product.default_discount\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_discount<\/span><span class=\"hljs-params\">(self, discount)<\/span>:<\/span>\n        self.discount = discount\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">net_price<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> self.price * (<span class=\"hljs-number\">1<\/span> - self.discount)\n\n\np1 = Product(<span class=\"hljs-number\">100<\/span>)\nprint(p1.net_price())\n <span class=\"hljs-comment\"># 100<\/span>\n\np2 = Product(<span class=\"hljs-number\">200<\/span>)\np2.set_discount(<span class=\"hljs-number\">0.05<\/span>)\nprint(p2.net_price())\n <span class=\"hljs-comment\"># 190<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li> A class attribute is shared by all instances of the class. To define a class attribute, you place it outside of the <code>__init__()<\/code> method.<\/li><li>Use <code>class_name.class_attribute<\/code> or <code>object_name.class_attribute<\/code> to access the value of the <code>class_attribute<\/code>. <\/li><li>Use class attributes for storing class contants, track data across all instances, and setting default values for all instances of the class.<\/li><\/ul>\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=\"440\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-attributes\/\"\n\t\t\t\tdata-post-title=\"Python Class Attributes\"\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=\"440\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class-attributes\/\"\n\t\t\t\tdata-post-title=\"Python Class Attributes\"\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 the Python class attributes and how to use them effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":417,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-440","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/440","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=440"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/440\/revisions"}],"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=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}