Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I want to know why are constructors certainly termed as "Constructors"? What is their end-goal and how are they different from functions in a class?

Also, can there be more than one __init__ in a class? I tried the following, can someone please explain the result?

>>> class test:

    def __init__(self):

        print "init 1"

    def __init__(self):

        print "init 2"

>>> s=test()

init 2

The last question is, can we overload the __init__?

1 Answer

0 votes
by (108k points)
edited by

Please be informed that you cannot perform method/function overloading in Python means you can't have duplicated function names but different parameters.

In your code sample, you are not performing the constructor overloading __init__(). The second constructor definition rebinds the name __init__ to the new function, rendering the first method inaccessible.

Kick-start your career in Python with the perfect Python Course.

Related questions

0 votes
1 answer
asked Jun 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
+1 vote
2 answers

Browse Categories

...