Python constructors and __init__

0 votes
Why are constructors called as "Constructors"? What is the purpose and how are they different from methods in a class?

Also, can there be more that one __init__ in a class? I tried something like 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
Finally, Is __init__ an operator overloader?
Oct 11, 2018 in Python by ana1504.k
• 7,870 points
129 views

1 answer to this question.

0 votes
There is no function overloading in Python, meaning that you can't have multiple functions with the same name but different arguments.

In your code example, you're not overloading __init__(). What happens is that the second definition rebinds the name __init__ to the new method, rendering the first method inaccessible.
answered Oct 11, 2018 by SDeb
• 13,180 points

Related Questions In Python

0 votes
1 answer

__init__ and self in python

The self variable represents an instance of ...READ MORE

answered Sep 17, 2018 in Python by SayantiniDeb
• 200 points
48 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
97 views
0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 17, 2018 in Python by Johnathon
• 9,080 points
51 views
+1 vote
3 answers
0 votes
1 answer

Examples for string find in Python

you can use str.index too: >>> 'sdfasdf'.index('cc') Traceback ...READ MORE

answered Aug 29, 2018 in Python by Priyaj
• 56,920 points
44 views
0 votes
1 answer

How to create new folder?

You can create a folder with os.makedirs() and use os.path.exists() to ...READ MORE

answered Sep 3, 2018 in Python by Priyaj
• 56,920 points
130 views
0 votes
1 answer
0 votes
1 answer

What is the fucntion of mkdir -p in Python

mkdir -p gives you an error if the ...READ MORE

answered Oct 30, 2018 in Python by Priyaj
• 56,920 points
110 views
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,180 points
38 views
0 votes
1 answer

Speech Recognition and Python

you can use https://pypi.python.org/pypi/pygsr $> pip install pygsr example ...READ MORE

answered Oct 29, 2018 in Python by SDeb
• 13,180 points
159 views