{"id":141,"date":"2024-01-25T13:38:31","date_gmt":"2024-01-25T13:38:31","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=141"},"modified":"2024-01-25T13:38:33","modified_gmt":"2024-01-25T13:38:33","slug":"python-__init__","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-__init__\/","title":{"rendered":"Python __init__"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>__init__()<\/code>&nbsp;method to initialize object\u2019s attributes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python __init__() method<\/h2>\n\n\n\n<p>When you create a new object of a\u00a0class, Python automatically calls the\u00a0<code>__init__()<\/code>\u00a0method to initialize the\u00a0object\u2019s attributes.<\/p>\n\n\n\n<p>Unlike regular\u00a0methods, the\u00a0<code>__init__()<\/code>\u00a0method has two underscores (__) on each side. Therefore, the\u00a0<code>__init__()<\/code>\u00a0is often called dunder init. The name comes abbreviation of the double underscores init.<\/p>\n\n\n\n<p>The double underscores at both sides of the&nbsp;<code>__init__()<\/code>&nbsp;method indicate that Python will use the method internally. In other words, you should not explicitly call this method.<\/p>\n\n\n\n<p>Since Python will automatically call the&nbsp;<code>__init__()<\/code>&nbsp;method immediately after creating a new object, you can use the&nbsp;<code>__init__()<\/code>&nbsp;method to initialize the object\u2019s attributes.<\/p>\n\n\n\n<p>The following defines the\u00a0<code>Person<\/code>\u00a0class with the\u00a0<code>__init__()<\/code>\u00a0method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, name, age): self.name = name self.age = age if __name__ == '__main__': person = Person('John', 25) print(f\"I'm {person.name}. I'm {person.age} years old.\") <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When you create an instance of the&nbsp;<code>Person<\/code>&nbsp;class, Python performs two things:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, create a new instance of the&nbsp;<code>Person<\/code>&nbsp;class by setting the object\u2019s namespace such as&nbsp;<code>__dict__<\/code>&nbsp;attribute to empty (<code>{}<\/code>).<\/li>\n\n\n\n<li>Second, call the&nbsp;<code>__init__<\/code>&nbsp;method to initialize the attributes of the newly created object.<\/li>\n<\/ul>\n\n\n\n<p>Note that the&nbsp;<code>__init__<\/code>&nbsp;method doesn\u2019t create the object but only initializes the object\u2019s attributes. Hence, the&nbsp;<code>__init__()<\/code>&nbsp;is not a constructor.<\/p>\n\n\n\n<p>If the&nbsp;<code>__init__<\/code>&nbsp;has parameters other than the&nbsp;<code>self<\/code>, you need to pass the corresponding arguments when creating a new object like the example above. Otherwise, you\u2019ll get an error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The __init__ method with default parameters<\/h3>\n\n\n\n<p>The\u00a0<code>__init__()<\/code>\u00a0method\u2019s parameters can have default values. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: def __init__(self, name, age=22): self.name = name self.age = age if __name__ == '__main__': person = Person('John') print(f\"I'm {person.name}. I'm {person.age} years old.\")<\/code><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>I'm John. I'm 22 years old.<\/code><small>Code language: JavaScript (javascript)<\/small><\/code><\/pre>\n\n\n\n<p>In this example, the&nbsp;<code>age<\/code>&nbsp;parameter has a default value of&nbsp;<code>22<\/code>. Because we don\u2019t pass an argument to the&nbsp;<code>Person()<\/code>, the&nbsp;<code>age<\/code>&nbsp;uses the default value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;__init__()&nbsp;method to initialize object\u2019s attributes. Introduction to the Python __init__() method When you create a new object of a\u00a0class, Python automatically calls the\u00a0__init__()\u00a0method to initialize the\u00a0object\u2019s attributes. Unlike regular\u00a0methods, the\u00a0__init__()\u00a0method has two underscores (__) on each side. Therefore, the\u00a0__init__()\u00a0is often called dunder init. The name [&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-141","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\/141","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=141"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/141\/revisions\/142"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}