{"id":1434,"date":"2023-05-11T12:20:00","date_gmt":"2023-05-11T06:50:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1434"},"modified":"2024-04-16T13:29:02","modified_gmt":"2024-04-16T07:59:02","slug":"init-vs-new","status":"publish","type":"post","link":"https:\/\/geekpython.in\/init-vs-new","title":{"rendered":"__init__ Vs __new__ Methods In Python &#8211; With Examples"},"content":{"rendered":"\n<p>You must have seen the implementation of the&nbsp;<code>__init__<\/code>&nbsp;method in any Python class, and if you have worked with Python classes, you must have implemented the&nbsp;<code>__init__<\/code>&nbsp;method many times. However, you are unlikely to have implemented or seen a&nbsp;<code>__new__<\/code>&nbsp;method within any class.<\/p>\n\n\n\n<p><strong>In this article, we&#8217;ll see:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Definition of the&nbsp;<code>__init__<\/code>&nbsp;and&nbsp;<code>__new__<\/code>&nbsp;methods<\/li>\n\n\n\n<li><code>__init__<\/code>&nbsp;method and&nbsp;<code>__new__<\/code>&nbsp;method implementation<\/li>\n\n\n\n<li>When they should be used<\/li>\n\n\n\n<li>The distinction between the two methods<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<p class=\"responsive-video-wrap clr\"><iframe loading=\"lazy\" title=\"__new__ VS __init__ in Python\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/ruOzNI9Q-Ho?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<\/div><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-init-vs-new-method\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-init-vs-new-method\"><\/a>__init__ Vs __new__ Method<\/h1>\n\n\n\n<p>The&nbsp;<code>__init__<\/code>&nbsp;method is an initializer method that is used to initialize the attributes of an object after it is created, whereas the&nbsp;<code>__new__<\/code>&nbsp;method is used to create the object.<\/p>\n\n\n\n<p>When we define both the&nbsp;<code>__new__<\/code>&nbsp;and the&nbsp;<code>__init__<\/code>&nbsp;methods inside a class, Python first calls the&nbsp;<code>__new__<\/code>&nbsp;method to create the object and then calls the&nbsp;<code>__init__<\/code>&nbsp;method to initialize the object&#8217;s attributes.<\/p>\n\n\n\n<p>Most programming languages require only a constructor, a special method to create and initialize objects, but Python has both a constructor and an initializer.<\/p>\n\n\n\n<p>Let&#8217;s talk about both these methods one by one and implement these methods inside a Python class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-new-method\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-new-method\"><\/a><strong>__new<\/strong>__ Method<\/h3>\n\n\n\n<p>As stated already, the&nbsp;<code>__new__<\/code>&nbsp;method is a constructor method used to create and return an object(instance of the class).<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<p><code>object.__new__(cls, *args, **kwargs)<\/code><\/p>\n\n\n\n<p>The&nbsp;<code>__new__<\/code>&nbsp;method&#8217;s first parameter is&nbsp;<code>cls<\/code>, which is a class of the object we want to create.<\/p>\n\n\n\n<p>The&nbsp;<code>*args<\/code>&nbsp;and&nbsp;<code>**kwargs<\/code>&nbsp;parameters are not used by the&nbsp;<code>__new__<\/code>&nbsp;method, but they must match the parameters of the class&#8217;s&nbsp;<code>__init__<\/code>&nbsp;method.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-geekpython-python-programming-tutorials wp-block-embed-geekpython-python-programming-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oceanwp-oembed-wrap clr\"><blockquote class=\"wp-embedded-content\" data-secret=\"IaWJpPeQre\"><a href=\"https:\/\/geekpython.in\/understanding-args-and-kwargs-in-python-best-practices-and-guide\">Understanding args and kwargs in Python: Best Practices and Guide<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Understanding args and kwargs in Python: Best Practices and Guide&#8221; &#8212; GeekPython - Python Programming Tutorials\" src=\"https:\/\/geekpython.in\/understanding-args-and-kwargs-in-python-best-practices-and-guide\/embed#?secret=zYpBNRZQgK#?secret=IaWJpPeQre\" data-secret=\"IaWJpPeQre\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Defined a base class\nclass Name:\n    # Created a __new__ method\n    def __new__(cls):\n        print(f'Called the __new__ method.')\n        return super(Name, cls).__new__(cls)\n\n    # Created an __init__ method\n    def __init__(self):\n        print(f\"Called the __init__ method.\")\n\n# Created an object\nName()<\/pre><\/div>\n\n\n\n<p>In the above code, we defined the&nbsp;<code>__new__<\/code>&nbsp;and&nbsp;<code>__init__<\/code>&nbsp;methods within the class&nbsp;<code>Name<\/code>. The&nbsp;<code>__new__<\/code>&nbsp;method accepts the&nbsp;<code>cls<\/code>&nbsp;parameter, which is used to refer to the class&nbsp;<code>Name<\/code>, and when called, it prints the message and returns the class instance using the&nbsp;<code>super(Name, cls).__new__(cls)<\/code>.<\/p>\n\n\n\n<p>One thing to note is that the&nbsp;<code>Name<\/code>&nbsp;class is a base class, so we could have directly called the&nbsp;<code>__new__<\/code>&nbsp;method on the object like this expression&nbsp;<code>object.__new__(cls)<\/code>. However, the standard method is to use the&nbsp;<code>super()<\/code>&nbsp;function.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-geekpython-python-programming-tutorials wp-block-embed-geekpython-python-programming-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oceanwp-oembed-wrap clr\"><blockquote class=\"wp-embedded-content\" data-secret=\"WorIce4UQh\"><a href=\"https:\/\/geekpython.in\/super-in-python\">How To Use super() Function Within Python Classes<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How To Use super() Function Within Python Classes&#8221; &#8212; GeekPython - Python Programming Tutorials\" src=\"https:\/\/geekpython.in\/super-in-python\/embed#?secret=190NZPnqEE#?secret=WorIce4UQh\" data-secret=\"WorIce4UQh\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<p>The&nbsp;<code>__init__<\/code>&nbsp;method is then called with the instance passed to the&nbsp;<code>self<\/code>&nbsp;parameter.<\/p>\n\n\n\n<p>Then we called the&nbsp;<code>Name<\/code>&nbsp;class (<code>Name()<\/code>), and when we run the code, we get the output shown below.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Called the __new__ method.\nCalled the __init__ method.<\/pre><\/div>\n\n\n\n<p>The output shows that the&nbsp;<code>__new__<\/code>&nbsp;method is called first and then the&nbsp;<code>__init__<\/code>&nbsp;method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-init-method\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-init-method\"><\/a>__init__ Method<\/h3>\n\n\n\n<p>As we saw in the above example, the&nbsp;<code>__init__<\/code>&nbsp;method is called to initialize the attributes of the object as soon as the object is created.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-geekpython-python-programming-tutorials wp-block-embed-geekpython-python-programming-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oceanwp-oembed-wrap clr\"><blockquote class=\"wp-embedded-content\" data-secret=\"fHq22qlhrd\"><a href=\"https:\/\/geekpython.in\/init-and-call-method\">__init__ and __call__ In Python &#8211; What Do They Do?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;__init__ and __call__ In Python &#8211; What Do They Do?&#8221; &#8212; GeekPython - Python Programming Tutorials\" src=\"https:\/\/geekpython.in\/init-and-call-method\/embed#?secret=WEJsMOGTqn#?secret=fHq22qlhrd\" data-secret=\"fHq22qlhrd\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<p><code>__init__(self, *args, **kwargs)<\/code><\/p>\n\n\n\n<p>As a first parameter, the&nbsp;<code>__init__<\/code>&nbsp;method accepts&nbsp;<code>self<\/code>, which is used to refer to the class instance.<\/p>\n\n\n\n<p>The parameters&nbsp;<code>*args<\/code>&nbsp;and&nbsp;<code>**kwargs<\/code>&nbsp;are used to initialize the instance variable with the values stored within them.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Defined a base class\nclass Name:\n    # Created a __new__ method\n    def __new__(cls, name):\n        print(f'Called the __new__ method.')\n        return super(Name, cls).__new__(cls)\n\n    # Created an __init__ method\n    def __init__(self, name):\n        print(f\"Called the __init__ method.\")\n        self.name = name\n\n# Created an object\nname_obj = Name('Sachin')\nprint(name_obj.name)<\/pre><\/div>\n\n\n\n<p>In the&nbsp;<code>__init__<\/code>&nbsp;method, we passed the&nbsp;<code>name<\/code>&nbsp;parameter and did the same in the&nbsp;<code>__new__<\/code>&nbsp;method to make the&nbsp;<code>__new__<\/code>&nbsp;and&nbsp;<code>__init__<\/code>&nbsp;method&nbsp;<a target=\"_blank\" href=\"https:\/\/python-forge.readthedocs.io\/en\/latest\/glossary.html#term-signature\" rel=\"noreferrer noopener\">signature<\/a>&nbsp;compatible with each other.<\/p>\n\n\n\n<p>We called the class with the&nbsp;<code>'Sachin'<\/code>&nbsp;argument, which will automatically invoke the&nbsp;<code>__init__<\/code>&nbsp;method and will initialize the instance variable&nbsp;<code>self.name<\/code>&nbsp;with this value.<\/p>\n\n\n\n<p>When we call the&nbsp;<code>name<\/code>&nbsp;attribute(instance variable) on the object&nbsp;<code>name_obj<\/code>, we&#8217;ll get the following output.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Called the __new__ method.\nCalled the __init__ method.\nSachin<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>name<\/code>&nbsp;attribute(instance variable) of the&nbsp;<code>name_obj<\/code>&nbsp;is initialized to the value&nbsp;<code>'Sachin'<\/code>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-implementation\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-implementation\"><\/a><strong>Implementation<\/strong><\/h1>\n\n\n\n<p>Let&#8217;s define both&nbsp;<code>__new__<\/code>&nbsp;and&nbsp;<code>__init__<\/code>&nbsp;methods inside the class&nbsp;<code>Language<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Language:\n    def __new__(cls, *args):\n        return super().__new__(cls)\n\n\n    def __init__(self, lang, year):\n        self.lang = lang\n        self.year = year\n\n\nlanguage = Language('Python', 1991)\nprint(language.lang)\nprint(language.year)\n\n----------\nPython\n1991<\/pre><\/div>\n\n\n\n<p>We defined both&nbsp;<code>__new__<\/code>&nbsp;and&nbsp;<code>__init__<\/code>&nbsp;methods inside the class&nbsp;<code>Language<\/code>&nbsp;and created the class object, when we run the code, Python will call the&nbsp;<code>__new__<\/code>&nbsp;method which is responsible for creating and returning the object of the class, and then calls the&nbsp;<code>__init__<\/code>&nbsp;method which is responsible for the initialization of the object&#8217;s attributes(instance variables).<\/p>\n\n\n\n<p>Now we can access the attributes of the object&nbsp;<code>lang<\/code>&nbsp;and&nbsp;<code>year<\/code>&nbsp;using dot notation on the object&nbsp;<code>language<\/code>&nbsp;as we did in the above code.<\/p>\n\n\n\n<p>Every time we create a new object, the&nbsp;<code>__init__<\/code>&nbsp;method is invoked, which means that if we don&#8217;t return&nbsp;<code>super().__new__(cls)<\/code>, then the&nbsp;<code>__init__<\/code>&nbsp;method will not execute and return&nbsp;<code>None<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Language:\n    def __new__(cls, *args):\n        print(\"Creating\")\n\n    # Method not called\n    def __init__(self, lang, year):\n        print(\"Initializing\")\n        self.lang = lang\n        self.year = year\n\n\nlanguage = Language('Python', 1991)\nprint(language)\n\n----------\nCreating\nNone<\/pre><\/div>\n\n\n\n<p><strong>Let&#8217;s see what happens when we implement only the<\/strong>&nbsp;<code>__init__<\/code>&nbsp;<strong>method inside a class.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Language:\n    def __init__(self, lang, year):\n        self.lang = lang\n        self.year = year\n\n\nlanguage = Language('Python', 1991)\nprint(language.lang)\nprint(language.year)\n\n----------\nPython\n1991<\/pre><\/div>\n\n\n\n<p>The code works the same as the previous code which we saw at the beginning of this section.<\/p>\n\n\n\n<p>When we instantiated the class using&nbsp;<code>language = Language('Python', 1991)<\/code>, the expression is equivalent to the following:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">language = object.__new__(Language)\nlanguage.__init__('Python', 1991)<\/pre><\/div>\n\n\n\n<p>If we try to print the&nbsp;<code>language<\/code>&nbsp;object after calling the&nbsp;<code>__new__<\/code>&nbsp;and the&nbsp;<code>__init__<\/code>&nbsp;methods using the&nbsp;<code>__dict__<\/code>, then we&#8217;ll get the following output:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">language = object.__new__(Language)\nprint(language.__dict__)\nlanguage.__init__('Python', 1991)\nprint(language.__dict__)\n\n----------\n{}\n{'lang': 'Python', 'year': 1991}<\/pre><\/div>\n\n\n\n<p>We got an empty dictionary after calling the&nbsp;<code>__new__<\/code>&nbsp;method because the object was created but not yet initialized, to initialize, we called the&nbsp;<code>__init__<\/code>&nbsp;method explicitly and got the values.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-when-to-use\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-when-to-use\"><\/a>When To Use<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-use-case-of-new\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-use-case-of-new\"><\/a>Use case of __new__<\/h3>\n\n\n\n<p>Consider the following example in which we are using the&nbsp;<code>__new__<\/code>&nbsp;method to customize the object at the instantiation.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Reverse(str):\n    def __new__(cls, sequence):\n        return super().__new__(cls, sequence[::-1])\n\nseq = Reverse(\"GeekPython\")\nprint(seq)<\/pre><\/div>\n\n\n\n<p>The above code defines the class&nbsp;<code>Reverse<\/code>, which inherits from the&nbsp;<code>str<\/code>&nbsp;built-in type, as well as the&nbsp;<code>__new__<\/code>&nbsp;method that accepts a&nbsp;<code>sequence<\/code>. We override the&nbsp;<code>__new__<\/code>&nbsp;method to reverse the&nbsp;<code>sequence<\/code>&nbsp;before creating the object.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">nohtyPkeeG<\/pre><\/div>\n\n\n\n<p>The argument&nbsp;<code>\"GeekPython\"<\/code>&nbsp;passed to the&nbsp;<code>Reverse<\/code>&nbsp;class got reversed due to&nbsp;<code>sequence[::-1]<\/code>&nbsp;before the object is created.<\/p>\n\n\n\n<p>This can&#8217;t be done using the&nbsp;<code>__init__<\/code>&nbsp;method, if we try to do so, the result will be an error.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Reverse(str):\n    def __init__(self, sequence):\n        super().__init__(sequence[::-1])\n\nseq = Reverse(\"GeekPython\")\nprint(seq)\n\n----------\nTypeError: object.__init__() takes exactly one argument (the instance to initialize)<\/pre><\/div>\n\n\n\n<p>Another use case of the&nbsp;<code>__new__<\/code>&nbsp;method is creating a&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Singleton_pattern\" target=\"_blank\">Singleton<\/a>&nbsp;<strong><em>(design pattern that restricts the instantiation of a class to a single instance).<\/em><\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Singleton:\n    # Created a private variable\n    __ins = None\n\n    # Defined the __new__ method\n    def __new__(cls):\n        if cls.__ins is None:\n            print(\"Instance creating...\")\n            cls.__ins = super().__new__(cls)\n        return cls.__ins\n\n# Creating object\nobj1 = Singleton()\nobj2 = Singleton()\nobj3 = Singleton()\nprint(obj1)\nprint(obj2)\nprint(obj3)\n\n# Checking if they are all same\nprint(obj1 is obj2 is obj3)<\/pre><\/div>\n\n\n\n<p>In the above code, we defined the class&nbsp;<code>Singleton<\/code>&nbsp;and created a private variable&nbsp;<code>__obj<\/code>&nbsp;to store the class&#8217;s single instance, as well as a&nbsp;<code>__new__<\/code>&nbsp;method that checks if the&nbsp;<code>__ins<\/code>&nbsp;is&nbsp;<code>None<\/code>, then creates a new instance and assigns it to the&nbsp;<code>__ins<\/code>, and returns the existing instance if the&nbsp;<code>__ins<\/code>&nbsp;is not&nbsp;<code>None<\/code>.<\/p>\n\n\n\n<p>Then we printed three instances of the&nbsp;<code>Singleton<\/code>&nbsp;class named&nbsp;<code>obj1<\/code>,&nbsp;<code>obj2<\/code>, and&nbsp;<code>obj3<\/code>&nbsp;and checked to see if they were all the same.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Instance creating...\n&lt;__main__.Singleton object at 0x000001B3DFD5C130&gt;\n&lt;__main__.Singleton object at 0x000001B3DFD5C130&gt;\n&lt;__main__.Singleton object at 0x000001B3DFD5C130&gt;\nTrue<\/pre><\/div>\n\n\n\n<p>All three instances point to the same memory address, and we can see that we got&nbsp;<code>True<\/code>, indicating that they are all the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-use-case-of-init\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-use-case-of-init\"><\/a>Use case of __init__<\/h3>\n\n\n\n<p>The&nbsp;<code>__init__<\/code>&nbsp;method is commonly used to initialize the object&#8217;s attributes with or without the default values.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Language:\n    def __init__(self, lang=\"Python\", year=1991):\n        self.lang = lang\n        self.year = year\n\n    def show(self):\n        print(f'Language: {self.lang} | Founded: {self.year}.')\n\nlanguage = Language()\nlanguage.show()<\/pre><\/div>\n\n\n\n<p>The above code defines a&nbsp;<code>Language<\/code>&nbsp;class and the&nbsp;<code>__init__<\/code>&nbsp;method, which accepts&nbsp;<code>lang<\/code>&nbsp;and&nbsp;<code>year<\/code>&nbsp;parameters with default values of&nbsp;<code>\"Python\"<\/code>&nbsp;and&nbsp;<code>1991<\/code>, respectively.<\/p>\n\n\n\n<p>When we call the&nbsp;<code>Language<\/code>&nbsp;class without argument, the&nbsp;<code>__init__<\/code>&nbsp;method will set the&nbsp;<code>lang<\/code>&nbsp;and&nbsp;<code>year<\/code>&nbsp;attributes to their default values.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Language: Python | Founded: 1991.<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-difference\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-difference\"><\/a>Difference<\/h1>\n\n\n\n<p>Now that we&#8217;ve seen the definition, syntax, and implementation of both methods, we are now able to differentiate between them.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><td>__new__ method<\/td><td>__init__ method<\/td><\/tr><\/thead><tbody><tr><td>The&nbsp;<code>__new__<\/code>&nbsp;method is called first<\/td><td>The&nbsp;<code>__init__<\/code>&nbsp;method is called after the&nbsp;<code>__new__<\/code>&nbsp;method<\/td><\/tr><tr><td>Used to create and return the object<\/td><td>Used to initialize the attributes of the object<\/td><\/tr><tr><td>It is a constructor method<\/td><td>It is an initializer method<\/td><\/tr><tr><td>Takes class as the first parameter<\/td><td>Takes the instance of the class as the first parameter<\/td><\/tr><tr><td>Can be overridden to customize the object at the instantiation<\/td><td>Probably only be used to initialize the attributes of the object<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/init-vs-new#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>Python has a concept of a constructor and an initializer method. The&nbsp;<code>__new__<\/code>&nbsp;method is a constructor method whereas the&nbsp;<code>__init__<\/code>&nbsp;method is an initializer method. Python first calls the&nbsp;<code>__new__<\/code>&nbsp;method which is responsible for the object creation and then calls the&nbsp;<code>__init__<\/code>&nbsp;method which is responsible for the initialization of the object&#8217;s attributes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/context-managers-and-python-with-statement\" rel=\"noreferrer noopener\">Context managers and the with statement in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/abc-in-python\" rel=\"noreferrer noopener\">What is abstract base class(ABC) in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/access-modifiers-in-python\" rel=\"noreferrer noopener\">Public, Protected, and Private access modifiers in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/class-inheritance-in-python\" rel=\"noreferrer noopener\">What are inheritance and different types of inheritance in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide\" rel=\"noreferrer noopener\">What is enumerate() function in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/exec-function-in-python\" rel=\"noreferrer noopener\">Execute dynamically generated code using the exec() in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/asyncio-how-to-use-asyncawait-in-python\" rel=\"noreferrer noopener\">Async\/Await &#8211; Asynchronous programming using asyncio in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You must have seen the implementation of the&nbsp;__init__&nbsp;method in any Python class, and if you have worked with Python classes, you must have implemented the&nbsp;__init__&nbsp;method many times. However, you are unlikely to have implemented or seen a&nbsp;__new__&nbsp;method within any class. In this article, we&#8217;ll see: __init__ Vs __new__ Method The&nbsp;__init__&nbsp;method is an initializer method that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1114,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,24],"tags":[26,12,31],"class_list":["post-1434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-object-oriented","tag-oop","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1434"}],"version-history":[{"count":4,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1434\/revisions"}],"predecessor-version":[{"id":1719,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1434\/revisions\/1719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1114"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}