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”, “rahul@gmail.com”)
print(st.name)
Output:
rahul