<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pila louis</title>
    <description>The latest articles on DEV Community by Pila louis (@pila).</description>
    <link>https://dev.to/pila</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F269491%2F66b27c4c-0dcf-4c70-b16c-0e7e346eadb6.jpg</url>
      <title>DEV Community: Pila louis</title>
      <link>https://dev.to/pila</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pila"/>
    <language>en</language>
    <item>
      <title>Constructors in Python (__init vs __new__)</title>
      <dc:creator>Pila louis</dc:creator>
      <pubDate>Wed, 16 Sep 2020 11:10:31 +0000</pubDate>
      <link>https://dev.to/pila/constructors-in-python-init-vs-new-2f9j</link>
      <guid>https://dev.to/pila/constructors-in-python-init-vs-new-2f9j</guid>
      <description>&lt;p&gt;Most object-oriented programming languages such as Java, C++, C#..etc have the concept of a constructor, a special method that creates and initializes the object when it is created. Python is a little different; it has a constructor and an initializer. The constructor function is rarely used unless you're doing something exotic. So, we'll start our discussion with the initialization method. &lt;/p&gt;

&lt;p&gt;The assumption in this article is that you already know the basics of classes and objects in python. &lt;/p&gt;

&lt;p&gt;The constructor function in python is called  &lt;code&gt;__new__&lt;/code&gt; and &lt;code&gt;__init__&lt;/code&gt; is the initializer function.&lt;/p&gt;

&lt;p&gt;Quoting the python documentation,  &lt;code&gt;__new__&lt;/code&gt; is used when you need to control the creation of a new instance while &lt;code&gt;__init__&lt;/code&gt; is used when you need to control the initialization of a new instance.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;__new__&lt;/code&gt; is the first step of instance creation. It's called first and is responsible for returning a new instance of your class.&lt;/p&gt;

&lt;p&gt;In contrast, &lt;code&gt;__init__&lt;/code&gt; doesn't return anything; it's only responsible for initializing the instance after it's been created. In general, you shouldn't need to override &lt;code&gt;__new__&lt;/code&gt;unless you're subclassing an immutable type like &lt;strong&gt;str, int, Unicode, or tuple.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; &lt;br&gt;
Never name a function of your own with leading and trailing double underscores. It may mean nothing to Python, but there's always the possibility that the designers of Python will add a function that has a special purpose with that name in the future, and when they do, your code will break.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1: Using &lt;code&gt;__init__&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Point:

    def __init__(self, data):
        self.num = data

    def print_num(self):
        print(self.num)



obj = Point(100)

obj.print_num()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;100&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The self parameter is a reference to the current instance of the class and is used to access variables that belong to the class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2:
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person:

    def __new__(cls):
        return object.__new__(cls)

    def __init__(self):
        self.instance_method()

    def instance_method(self):
        print('success!')

personObj = Person()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;__init__&lt;/code&gt; receives the argument self, while &lt;code&gt;__new__&lt;/code&gt; receives the class &lt;code&gt;(cls&lt;/code&gt;). Since &lt;code&gt;self&lt;/code&gt; is a reference to the instance, this should tell you quite evidently that the instance is already created by the time &lt;code&gt;__init__&lt;/code&gt; gets called, since it gets passed the instance. It's also possible to call instance methods precisely because the instance has already been created.&lt;/p&gt;

&lt;p&gt;Thank you for reading. 😄&lt;br&gt;
END!!!&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
