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
• 6,750 points
81 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 10,940 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
23 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,710 points
51 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,070 points
23 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,140 points
25 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,140 points
103 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,140 points
49 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
• 10,940 points
29 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
• 10,940 points
92 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.