{"id":143,"date":"2024-01-25T13:41:51","date_gmt":"2024-01-25T13:41:51","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=143"},"modified":"2024-01-25T13:41:55","modified_gmt":"2024-01-25T13:41:55","slug":"python-instance-variables","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-instance-variables\/","title":{"rendered":"Python Instance Variables"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python instance variables including data variables and function variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python instance variables<\/h2>\n\n\n\n<p>In Python,\u00a0class variables\u00a0are bound to a\u00a0class\u00a0while instance variables are bound to a specific instance of a class. The instance variables are also called instance attributes.<\/p>\n\n\n\n<p>The following defines a\u00a0<code>HtmlDocument<\/code>\u00a0class with two class variables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from pprint import pprint class HtmlDocument: version = 5 extension = 'html' pprint(HtmlDocument.__dict__) print(HtmlDocument.extension) print(HtmlDocument.version)<\/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>mappingproxy({'__dict__': &lt;attribute '__dict__' of 'HtmlDocument' objects>, '__doc__': None, '__module__': '__main__', '__weakref__': &lt;attribute '__weakref__' of 'HtmlDocument' objects>, 'extension': 'html', 'version': 5})<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>HtmlDocument<\/code>&nbsp;class has two class variables:&nbsp;<code>extension<\/code>&nbsp;and&nbsp;<code>version<\/code>. Python stores these two variables in the&nbsp;<code>__dict__<\/code>&nbsp;attribute.<\/p>\n\n\n\n<p>When you access the class variables via the class, Python looks them up in the&nbsp;<code>__dict__<\/code>&nbsp;of the class.<\/p>\n\n\n\n<p>The following creates a new instance of the\u00a0<code>HtmlDocument<\/code>\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>home = HtmlDocument()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>home<\/code>\u00a0is an instance of the\u00a0<code>HtmlDocument<\/code>\u00a0class. It has its own\u00a0<code>__dict__<\/code>\u00a0attribute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>pprint(home.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>home.__dict__<\/code>\u00a0is now empty:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>home.__dict__<\/code>&nbsp;stores the instance variables of the&nbsp;<code>home<\/code>&nbsp;object like the&nbsp;<code>HtmlDocument.__dict__<\/code>&nbsp;stores the class variables of the&nbsp;<code>HtmlDocument<\/code>&nbsp;class.<\/p>\n\n\n\n<p>Unlike the\u00a0<code>__dict__<\/code>\u00a0attribute of a class, the type of the\u00a0<code>__dict__<\/code>\u00a0attribute of an instance is a\u00a0dictionary. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(type(home.__dict__))<\/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>&lt;class 'dict'><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Since a\u00a0dictionary\u00a0is\u00a0mutable, you can mutate it e.g., adding a new element to the dictionary.<\/p>\n\n\n\n<p>Python allows you to access the\u00a0class variables\u00a0from an instance of a class. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(home.extension) print(home.version)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In this case, Python looks up the variables extension and version in&nbsp;<code>home.__dict__<\/code>&nbsp;first. If it doesn\u2019t find them there, it\u2019ll go up to the class and look up in the&nbsp;<code>HtmlDocument.__dict__<\/code>.<\/p>\n\n\n\n<p>However, if Python can find the variables in the&nbsp;<code>__dict__<\/code>&nbsp;of the instance, it won\u2019t look further in the&nbsp;<code>__dict__<\/code>&nbsp;of the class.<\/p>\n\n\n\n<p>The following defines the\u00a0<code>version<\/code>\u00a0variable in the\u00a0<code>home<\/code>\u00a0object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>home.version = 6<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Python adds the\u00a0<code>version<\/code>\u00a0variable to the\u00a0<code>__dict__<\/code>\u00a0attribute of the\u00a0<code>home<\/code>\u00a0object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(home.__dict__)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>__dict__<\/code>\u00a0now contains one instance variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>{'version': 6}<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you access the version attribute of the\u00a0<code>home<\/code>\u00a0object, Python will return the value of the\u00a0<code>version<\/code>\u00a0in the\u00a0<code>home.__dict__<\/code>\u00a0dictionary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(home.version)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<code>6<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<p>If you change the class variables, these changes also reflect in the instances of the class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>HtmlDocument.media_type = 'text\/html' print(home.media_type)<\/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>text\/html<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Initializing instance variables<\/h2>\n\n\n\n<p>In practice, you initialize instance variables for all instances of a class in the\u00a0<code>__init__<\/code>\u00a0method.<\/p>\n\n\n\n<p>For example, the following redefines the\u00a0<code>HtmlDocument<\/code>\u00a0class that has two instance variables\u00a0<code>name<\/code>\u00a0and\u00a0<code>contents<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class HtmlDocument: version = 5 extension = 'html' def __init__(self, name, contents): self.name = name self.contents = contents<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When creating a new instance of the\u00a0<code>HtmlDocument<\/code>, you need to pass the corresponding arguments like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>blank = HtmlDocument('Blank', '')<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python instance variables including data variables and function variables. Introduction to the Python instance variables In Python,\u00a0class variables\u00a0are bound to a\u00a0class\u00a0while instance variables are bound to a specific instance of a class. The instance variables are also called instance attributes. The following defines a\u00a0HtmlDocument\u00a0class with two class variables: [&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-143","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\/143","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=143"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":144,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/143\/revisions\/144"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}