{"id":147,"date":"2024-01-25T13:45:57","date_gmt":"2024-01-25T13:45:57","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=147"},"modified":"2024-01-25T13:45:58","modified_gmt":"2024-01-25T13:45:58","slug":"python-class-attributes","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-class-attributes\/","title":{"rendered":"Python Class Attributes"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about the Python class attributes and when to use them appropriately.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to class attributes<\/h2>\n\n\n\n<p>Let\u2019s start with a simple\u00a0<code>Circle<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Circle: def __init__(self, radius): self.pi = 3.14159 self.radius = radius def area(self): return self.pi * self.radius**2 def circumference(self): return 2*self.pi * self.radius <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>Circle<\/code>&nbsp;class has two attributes&nbsp;<code>pi<\/code>&nbsp;and&nbsp;<code>radius<\/code>. It also has two methods that calculate the area and circumference of a circle.<\/p>\n\n\n\n<p>Both&nbsp;<code>pi<\/code>&nbsp;and&nbsp;<code>radius<\/code>&nbsp;are called&nbsp;<strong>instance attributes<\/strong>. In other words, they belong to a specific instance of the&nbsp;<code>Circle<\/code>&nbsp;class. If you change the attributes of an instance, it won\u2019t affect other instances.<\/p>\n\n\n\n<p>Besides instance attributes, Python also supports&nbsp;<strong>class attributes<\/strong>. The class attributes don\u2019t associate with any specific instance of the class. But they\u2019re shared by all instances of the class.<\/p>\n\n\n\n<p>If you\u2019ve been programming in Java or C#, you\u2019ll 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\u00a0<code>__init__()<\/code>\u00a0method. For example, the following defines\u00a0<code>pi<\/code>\u00a0as a class attribute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Circle: pi = 3.14159 def __init__(self, radius): self.radius = radius def area(self): return self.pi * self.radius**2 def circumference(self): return 2 * self.pi * self.radius <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\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\n<pre class=\"wp-block-code\"><code><code>object_name.class_attribute class_name.class_attribute<\/code><small>Code language: Oracle Rules Language (ruleslanguage)<\/small><\/code><\/pre>\n\n\n\n<p>In the&nbsp;<code>area()<\/code>&nbsp;and&nbsp;<code>circumference()<\/code>&nbsp;methods, we access the&nbsp;<code>pi<\/code>&nbsp;class attribute via the&nbsp;<code>self<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>Outside the\u00a0<code>Circle<\/code>\u00a0class, you can access the\u00a0<code>pi<\/code>\u00a0class attribute via an instance of the\u00a0<code>Circle<\/code>\u00a0class or directly via the\u00a0<code>Circle<\/code>\u00a0class. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>c = Circle(10) print(c.pi) print(Circle.pi)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>3.14159 3.14159<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Python class attributes work<\/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\u2019t 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\u00a0<code>Test<\/code>\u00a0class to demonstrate how Python handles instance and class attributes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Test: x = 10 def __init__(self): self.x = 20 test = Test() print(test.x) <em># 20<\/em> print(Test.x) <em># 10<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>The&nbsp;<code>Test<\/code>&nbsp;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&nbsp;<code>x<\/code>&nbsp;attribute via the instance of the&nbsp;<code>Test<\/code>&nbsp;class, it returns 20 which is the variable of the instance attribute.<\/p>\n\n\n\n<p>However, when we access the&nbsp;<code>x<\/code>&nbsp;attribute via the&nbsp;<code>Test<\/code>&nbsp;class, it returns 10 which is the value of the&nbsp;<code>x<\/code>&nbsp;class attribute.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When to use Python class attributes<\/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\">1) Storing class constants<\/h3>\n\n\n\n<p>Since a constant doesn\u2019t change from instance to instance of a class, it\u2019s handy to store it as a class attribute.<\/p>\n\n\n\n<p>For example, the&nbsp;<code>Circle<\/code>&nbsp;class has the&nbsp;<code>pi<\/code>&nbsp;constant that is the same for all instances of the class. Therefore, it\u2019s a good candidate for the class attributes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Tracking data across of all instances<\/h3>\n\n\n\n<p>The following adds the\u00a0<code>circle_list<\/code>\u00a0class attribute to the\u00a0<code>Circle<\/code>\u00a0class. When you create a new instance of the\u00a0<code>Circle<\/code>\u00a0class, the constructor adds the instance to the list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Circle: circle_list = &#91;] pi = 3.14159 def __init__(self, radius): self.radius = radius <em># add the instance to the circle list<\/em> self.circle_list.append(self) def area(self): return self.pi * self.radius**2 def circumference(self): return 2 * self.pi * self.radius c1 = Circle(10) c2 = Circle(20) print(len(Circle.circle_list)) <em># 2<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3) Defining default values<\/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\u00a0<code>Product<\/code>\u00a0class. All the instances of the\u00a0<code>Product<\/code>\u00a0class will have a default discount specified by the\u00a0<code>default_discount<\/code>\u00a0class attribute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Product: default_discount = 0 def __init__(self, price): self.price = price self.discount = Product.default_discount def set_discount(self, discount): self.discount = discount def net_price(self): return self.price * (1 - self.discount) p1 = Product(100) print(p1.net_price()) <em># 100<\/em> p2 = Product(200) p2.set_discount(0.05) print(p2.net_price()) <em># 190<\/em><\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about the Python class attributes and when to use them appropriately. Introduction to class attributes Let\u2019s start with a simple\u00a0Circle\u00a0class: The&nbsp;Circle&nbsp;class has two attributes&nbsp;pi&nbsp;and&nbsp;radius. It also has two methods that calculate the area and circumference of a circle. Both&nbsp;pi&nbsp;and&nbsp;radius&nbsp;are called&nbsp;instance attributes. In other words, they belong to a specific [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-1-classes-and-objects"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/147\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}