Overloading constructors or methods in python

How to overload constructors or methods in Python?

Python's constructor: _init__ () is the first method of a class. Whenever we try to instantiate an object __init__() is automatically invoked by python to initialize members of an object. We can't overload constructors or methods in Python. It shows an error if we try to overload.



class student:  
    def __init__(self,name):  
        self.name = name  
    def __init__(self, name, email):  
        self.name = name  
        self.email = email  
       
# This line will generate an error  
#st = student("rahul")  
  
# This line will call the second constructor  
st = student("rahul", "[email protected]")  
print(st.name)  
Output:  
rahul  

Posted on by