{"id":5163,"date":"2020-11-19T09:12:40","date_gmt":"2020-11-19T03:42:40","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=5163"},"modified":"2023-12-31T17:21:17","modified_gmt":"2023-12-31T11:51:17","slug":"python-multiple-constructors","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/","title":{"rendered":"Ways to Achieve Multiple Constructors in Python"},"content":{"rendered":"\n<p>Hello Programmers, today\u2019s article is about multiple constructors in Python. Switching to Python from any other programming language has all advantages except when it comes to multiple constructors. Python does not support multiple constructors. <strong>However, Python offers some alternative ways to support multiple constructors. We will discuss some of those ways here.<\/strong> But before that, let me brief you about what is the need for multiple constructors in a program.<\/p>\n\n\n\n<p><strong>Multiple constructions help you to customize your class according to its parameters. Upon using a different number of parameters, different constructions can be triggered. Unlike other programming languages, Python has a different way of handling multiple parameters. We&#8217;ll have a look at each of them in detail.<\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#Python_Multiple_Constructors_And_Its_Needs\" >Python Multiple Constructors And Its Needs:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#Different_ways_to_get_Multiple_Constructors_in_Python_are\" >Different ways to get Multiple Constructors in Python are:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#Python_Constructor_overloading_based_on_arguments_as_Multiple_Constructors\" >Python Constructor overloading based on arguments as Multiple Constructors:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#Python_Methods_calling_from_init_as_Multiple_Constructors\" >Python Methods calling from __init__ as  Multiple Constructors:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#classmethod_decorator_as_Multiple_Constructors\" >@classmethod decorator as Multiple Constructors:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#Must_Read\" >Must Read<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#Conclusion\" >Conclusion:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#Trending_Python_Articles\" >Trending Python Articles<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"block-6bb2de7c-1876-44ec-9cc5-5f571674cc56\"><span class=\"ez-toc-section\" id=\"Python_Multiple_Constructors_And_Its_Needs\"><\/span>Python Multiple Constructors And Its Needs:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"block-79ad5dcf-e168-454e-9b6d-b22d9360c24a\">Multiple constructors come into use when a defined class has to perform different functions. And on different parameters or instances of a class.<\/p>\n\n\n\n<p id=\"block-5b695f1d-2a4e-4422-87e8-5e93cfa86b9d\">On writing the same method multiple times for a class, the last one overwrites all the previous constructors. Since multiple constructors do not work well with Python, the class constructors exhibit polymorphism. This method helps to replicate the multiple constructor feature in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-6051f91a-c492-450f-9abd-ebc97c05310a\"><span class=\"ez-toc-section\" id=\"Different_ways_to_get_Multiple_Constructors_in_Python_are\"><\/span>Different ways to get Multiple Constructors in Python are:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-bf5602b7-78e9-4342-9cd0-f926c3d97bd3\">\n<li><strong>Constructor overloading based on arguments<\/strong><\/li>\n\n\n\n<li><strong>Methods calling from <a href=\"http:\/\/www.pythonpool.com\/init-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">__init__<\/a><\/strong><\/li>\n\n\n\n<li><strong>@classmethod decorator<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-4f01146e-74df-4eca-9941-fbfc63231055\"><span class=\"ez-toc-section\" id=\"Python_Constructor_overloading_based_on_arguments_as_Multiple_Constructors\"><\/span>Python Constructor overloading based on arguments as Multiple Constructors:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"block-0f39d4ec-16c7-40a0-8a95-e21e4e1f15d9\"><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass example: \n  \n    # constructor overloading \n    # based on args \n    def __init__(self, *args): \n  \n        # if args are more than 1 \n        # sum of args \n        if len(args) &gt; 1: \n            self.answer = 0\n            for i in args: \n                self.answer += i \n  \n        # if arg is an integer \n        # square the arg \n        elif isinstance(args&#x5B;0], integer): \n            self.answer = args&#x5B;0]*args&#x5B;0] \n  \n        # if arg is string \n        # Print with hello\n        elif isinstance(args&#x5B;0], str): \n            self.answer = &quot;Hello! &quot;+args&#x5B;0]+&quot;.&quot;\n  \n  \ne1 = example(1, 2, 3, 6, 8) \nprint(&quot;Sum of list :&quot;, e1.answer) \n  \ne2 = example(6) \nprint(&quot;Square of integer :&quot;, e2.answer) \n  \ne3 = example(&quot;Programmers&quot;) \nprint(&quot;String :&quot;, e3.answer) \n<\/pre><\/div>\n\n\n<p><strong>OUTPUT:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSum of list : 20\nSquare of integer : 36\nString : Hello! Programmers\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation:<\/strong><\/h3>\n\n\n\n<p>In the above example, the answer is the instance variable of the class example. Its value differed in different instances inside the class based on arguments. A class can have multiple arguments. Therefore, *args is defined as a tuple that holds different arguments passed to it.<\/p>\n\n\n\n<p>The arguments are accessible using an index. For instance, as in the integer and string case, since only one argument is passed, it is thus accessed as args[0]. For the <a href=\"http:\/\/www.pythonpool.com\/python-sum\/\">sum<\/a>, more than one argument passed to it is accessed by using a loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_Methods_calling_from_init_as_Multiple_Constructors\"><\/span>Python Methods calling from __init__ as  Multiple Constructors:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass equations: \n  \n  # single constructor to call other methods \n    def __init__(self, *abc): \n  \n        # when 2 arguments are passed \n        if len(abc) == 2: \n            self.ans = self.eq1(abc) \n  \n        # when 3 arguments are passed \n        elif len(abc) == 3: \n            self.ans = self.eq2(abc) \n  \n        # when more than 3 arguments are passed \n        else: \n            self.ans = self.eq3(abc) \n  \n     def eq1(self, args): \n        x = (args&#x5B;0]*args&#x5B;0])+(args&#x5B;1]*args&#x5B;1]) \n        return y \n\n    def eq2(self, args): \n        y = args&#x5B;0]+args&#x5B;1]-args&#x5B;2] \n        return x \n  \n    def eq3(self, args): \n        temp = 0\n        for i in range(0, len(args)): \n            temp += args&#x5B;i]*args&#x5B;i] \n          \n        temp = temp\/5.0 \n        z = temp \n        return z \n  \n  \nabc1 = equations(4, 2) \nabc2 = equations(4, 2, 3) \nabc3 = equations(1, 2, 3, 4, 5) \n  \nprint(&quot;equation 1 :&quot;, abc1.ans) \nprint(&quot;equation 2 :&quot;, abc2.ans) \nprint(&quot;equation 3 :&quot;, abc3.ans) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nequation 1 : 12\nequation 2 : 17\nequation 3 : 11.0\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation:<\/strong><\/h3>\n\n\n\n<p>In this example, three equations performed in the instances of the class are equaton1 &#8211; <strong>x= a<sup>2 <\/sup>+ b<sup>2<\/sup> <\/strong> equation2 &#8211; <strong>y = a<sup> <\/sup>+ b\u00a0&#8211; c<\/strong>.  Equation3 &#8211; z =<strong> sum of the square of arguments passed \/ 5.0<\/strong>. <\/p>\n\n\n\n<p>equation1 is for two arguments. equation2 is for three, and equation 3 is for more than three.<\/p>\n\n\n\n<p>Using a multi-constructor in Python, a class having one constructor __init__ is defined. It can perform any action on the creation of different class instances. Above all, in the above code, equations to be evaluated are written in different instances of the class. Above all, the __init__ constructor calls different methods to return the answer based on the number of arguments passed. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"classmethod_decorator_as_Multiple_Constructors\"><\/span>@classmethod decorator as Multiple Constructors:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass equations: \n  \n    # basic constructor \n    def __init__(self, x): \n        self.ans = x \n\n    @classmethod\n    def eq1(obj, args): \n        \n      # create an object for the class to return \n        a = obj((args&#x5B;0]*args&#x5B;0])+(args&#x5B;1]*args&#x5B;1])\n        return a \n \n    @classmethod\n    def eq2(obj, args): \n        b = obj(args&#x5B;0]+ args&#x5B;1] - args&#x5B;2]) \n        return b \n  \n    @classmethod\n    def eq3(obj, args): \n        temp = 0\n  \n        # square of each element \n        for i in range(0, len(args)): \n            temp += args&#x5B;i]*args&#x5B;i] \n  \n        temp = temp\/5.0 \n        z = obj(temp) \n        return z \n  \n  \nli = &#x5B;&#x5B;4, 2], &#x5B;4, 2, 3], &#x5B;1, 2, 3, 4, 5]] \ni = 0\n  \n# loop to get input three times \nwhile i &lt; 3: \n  \n    input = li&#x5B;i] \n  \n    # no.of.arguments = 2 \n    if len(input) == 2: \n        p = equations.eq1(input) \n        print(&quot;equation 1 :&quot;, p.ans) \n  \n    # no.of.arguments = 3 \n    elif len(input) == 3: \n        p = equations.eq1(input) \n        print(&quot;equation 2 :&quot;, p.ans) \n  \n    # More than three arguments \n    else: \n        p = equations.eq3(input) \n        print(&quot;equation 3 :&quot;, p.ans) \n  \n    #increment loop         \n    i += 1\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nequation 1 : 12\nequation 2 : 17\nequation 3 : 11.0\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation:<\/strong><\/h3>\n\n\n\n<p>Equations performed in the above example are: equaton1 &#8211; <strong>x= a<sup>2 <\/sup>+ b<sup>2<\/sup><\/strong>. Equation2 &#8211; <strong>y = a+ b\u00a0&#8211; c<\/strong>. Similarly, Equation3 &#8211; z =<strong> sum of the square of arguments passed \/ 5.0<\/strong>.<\/p>\n\n\n\n<p>When two arguments are passed, equation1 is evaluated. For three arguments, equation2 is performed. For more than three arguments, equation3 is evaluated.<\/p>\n\n\n\n<p>Instances are not created for the above class initially. Similarly, class methods are defined to evaluate the various equations using the @classmethod decorator. Therefore, they are now called using class names. In addition, we create objects inside the class methods after evaluating the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dynamic_programming\" target=\"_blank\" rel=\"noreferrer noopener\">equations<\/a>. Therefore, the instance variable returns the answer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Must_Read\"><\/span> Must Read<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"http:\/\/www.pythonpool.com\/python-super\/\">Introduction to Python Super With Examples<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/python-help-function\/\">Python Help Function<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/python-sys-exit\/\">Why is Python sys.exit better than other exit functions?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/python-bitstring\/\">Python Bitstring: Classes and Other Examples | Module<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In conclusion, we can say Python cannot support using multiple constructors for a class. It allows different alternatives discussed above.  However, constructor overloading<strong> <\/strong>and <strong><a href=\"http:\/\/www.pythonpool.com\/init-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">__init__ <\/a>definition<\/strong> incur certain problems. Firstly, there is no clear indication of what is required while creating class instances. Also, different combinations of initializing a new instance by passing arguments exist. The best out of the three alternatives given is thus decorating with <strong>@classmethod decorators <\/strong>as multi-constructors.<\/p>\n\n\n\n<p>However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.<\/p>\n\n\n\n<p><strong><em>Happy Pythoning!<\/em><\/strong><\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-delta monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-2\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Python_Articles\"><\/span>Trending Python Articles<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 11, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined.webp 1200w \" alt=\"[Fixed] nameerror: name Unicode is not defined\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method.webp 1200w \" alt=\"[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>\n\n\n<p>          <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Programmers, today\u2019s article is about multiple constructors in Python. Switching to Python from any other programming language has all advantages except when it comes &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Ways to Achieve Multiple Constructors in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#more-5163\" aria-label=\"More on Ways to Achieve Multiple Constructors in Python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":5290,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[2467,2465,2464,2466,2463,2468],"class_list":["post-5163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-can-you-have-multiple-constructors-python-class","tag-multiple-constructors-python","tag-python-class-multiple-constructors","tag-python-define-multiple-constructors","tag-python-multiple-constructors","tag-python-pass-class-multiple-constructors","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ways to Achieve Multiple Constructors in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"Python offers some of the alternative ways to support multiple constructors. We will discuss some of those ways here through practicals.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ways to Achieve Multiple Constructors in Python\" \/>\n<meta property=\"og:description\" content=\"Hello Programmers, today\u2019s article is about multiple constructors in Python. Switching to Python from any other programming language has all advantages\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-19T03:42:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-31T11:51:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Python Pool\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Python Pool\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/\"},\"author\":{\"name\":\"Python Pool\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998\"},\"headline\":\"Ways to Achieve Multiple Constructors in Python\",\"datePublished\":\"2020-11-19T03:42:40+00:00\",\"dateModified\":\"2023-12-31T11:51:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/\"},\"wordCount\":683,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png\",\"keywords\":[\"can you have multiple constructors python class\",\"multiple constructors python\",\"python class multiple constructors\",\"python define multiple constructors\",\"python multiple constructors\",\"python pass class multiple constructors\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/\",\"name\":\"Ways to Achieve Multiple Constructors in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png\",\"datePublished\":\"2020-11-19T03:42:40+00:00\",\"dateModified\":\"2023-12-31T11:51:17+00:00\",\"description\":\"Python offers some of the alternative ways to support multiple constructors. We will discuss some of those ways here through practicals.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png\",\"width\":1350,\"height\":650,\"caption\":\"python multiple constructors\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ways to Achieve Multiple Constructors in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998\",\"name\":\"Python Pool\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"caption\":\"Python Pool\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Ways to Achieve Multiple Constructors in Python - Python Pool","description":"Python offers some of the alternative ways to support multiple constructors. We will discuss some of those ways here through practicals.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/","og_locale":"en_US","og_type":"article","og_title":"Ways to Achieve Multiple Constructors in Python","og_description":"Hello Programmers, today\u2019s article is about multiple constructors in Python. Switching to Python from any other programming language has all advantages","og_url":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/","og_site_name":"Python Pool","article_published_time":"2020-11-19T03:42:40+00:00","article_modified_time":"2023-12-31T11:51:17+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png","type":"image\/png"}],"author":"Python Pool","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Python Pool","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/"},"author":{"name":"Python Pool","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998"},"headline":"Ways to Achieve Multiple Constructors in Python","datePublished":"2020-11-19T03:42:40+00:00","dateModified":"2023-12-31T11:51:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/"},"wordCount":683,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png","keywords":["can you have multiple constructors python class","multiple constructors python","python class multiple constructors","python define multiple constructors","python multiple constructors","python pass class multiple constructors"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-multiple-constructors\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/","url":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/","name":"Ways to Achieve Multiple Constructors in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png","datePublished":"2020-11-19T03:42:40+00:00","dateModified":"2023-12-31T11:51:17+00:00","description":"Python offers some of the alternative ways to support multiple constructors. We will discuss some of those ways here through practicals.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-multiple-constructors\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/11\/Theatre-Actor-Portfolio-Website-9.png","width":1350,"height":650,"caption":"python multiple constructors"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-multiple-constructors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Ways to Achieve Multiple Constructors in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998","name":"Python Pool","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","caption":"Python Pool"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/5163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=5163"}],"version-history":[{"count":29,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/5163\/revisions"}],"predecessor-version":[{"id":31393,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/5163\/revisions\/31393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/5290"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=5163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=5163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=5163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}