{"id":6795,"date":"2020-07-11T19:04:45","date_gmt":"2020-07-11T19:04:45","guid":{"rendered":"https:\/\/www.askpython.com\/?p=6795"},"modified":"2020-07-11T19:04:47","modified_gmt":"2020-07-11T19:04:47","slug":"class-and-instance-attributes","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/oops\/class-and-instance-attributes","title":{"rendered":"Python Class Attribute and Instance Attribute"},"content":{"rendered":"\n<p>In this article, we will be focusing on <strong>Python Class Attribute and Instance Attribute<\/strong>.<\/p>\n\n\n\n<p>Attributes are the key-players of a programming language. They are responsible for holding important data values and also help in data manipulation.<\/p>\n\n\n\n<p>Let us now get started!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Python Class Attribute<\/h2>\n\n\n\n<p><code>Python Class Attribute<\/code> is an attribute\/variable that is enclosed within a Class. That is, its scope lies within the <a href=\"https:\/\/www.askpython.com\/python\/oops\/python-classes-objects\" class=\"rank-math-link\">Python class<\/a>.<\/p>\n\n\n\n<p>The Class attribute <strong>creates only a single copy of itself<\/strong> and this single copy is shared and utilized by all the functions and objects within that particular class.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Class-name:\n     variable = value\n<\/pre><\/div>\n\n\n<p>Let us now understand the implementation of the same through the below example.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Implementing the Class Attribute with an example<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass class_attribute: \n\tval = 1\n\n\tdef product(self): \n\t\tclass_attribute.val *= 10\n\t\tprint(class_attribute.val)\n\nobj1 = class_attribute() \nobj1.product()\t\t \n \nobj2 = class_attribute() \nobj2.product()\t\t \n<\/pre><\/div>\n\n\n<p>In this example, we create a class variable &#8216;val&#8217; and initialize it to 1.<\/p>\n\n\n\n<p>Further, we access the variable &#8216;val&#8217; within the function product() and manipulate the value by multiplying it with 10. <\/p>\n\n\n\n<p>As clearly observed, the same copy of the variable &#8216;val&#8217; is used by both of the objects created. <strong>Thus at first, val = 1. <\/strong><\/p>\n\n\n\n<p>When the object obj1 calls the function, the same copy of &#8216;val&#8217; is used(the value does not get reset) and thus it becomes val=10. On being called by the obj2, val becomes val*10 i.e. 10*10 = 100.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n10\n100\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Python Instance Attribute<\/h2>\n\n\n\n<p><code>Python Instance attribute<\/code> is a local attribute\/variable whose scope lies within the particular function that is using the attribute. Thus, it is enclosed by a particular function.<\/p>\n\n\n\n<p>The Instance attribute <strong>creates a new copy of itself<\/strong>, each time when it is being called by a <a href=\"https:\/\/www.askpython.com\/python\/python-functions\" class=\"rank-math-link\">function<\/a>\/object. That is, a distinct copy of this variable gets utilized every time the object or function tries to access it.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef function-name():\n    variable = value\n<\/pre><\/div>\n\n\n<p>Let us now implement local attributes with the help of an example.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Implementing Instance Attribute with an example<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass instance_attribute: \n\n\tdef product(self): \n\t   val = 20\n\t   val *= 10\n\t   print(val)\n\n\nobj1 = instance_attribute() \nobj1.product()\t\t \n \nobj2 = instance_attribute() \nobj2.product()\n<\/pre><\/div>\n\n\n<p>In this example, we have declared and initialized an instance attribute as val = 20. <\/p>\n\n\n\n<p>Further, when obj1 tries to access the variable through the function, it creates its own new copy as resets the default value to the initialized value and then provides access to it. <\/p>\n\n\n\n<p>The same scenario gets repeated when obj2 tries to access the instance variable &#8216;val&#8217;.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n200\n200\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3\/tutorial\/classes.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python Attributes &#8212; Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be focusing on Python Class Attribute and Instance Attribute. Attributes are the key-players of a programming language. They are responsible for holding important data values and also help in data manipulation. Let us now get started! Understanding Python Class Attribute Python Class Attribute is an attribute\/variable that is enclosed within [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":6801,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-6795","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\/6795","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=6795"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/6795\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/6801"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=6795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=6795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=6795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}