Default constructor in Python programming is a constructor which has no parameters. The default constructor will be called at the time of creating object of that class.
class cls: #'cls' is name of class.
def __init__(self): #this is Default Constructor.
print("This is Default Constructor")
def fun(self): #this is function.
print("This is Simple Function")
obj = cls() #'obj' is object of class 'cls', and calling default Constructor.
obj.fun()
class cls: #'cls' is name of class.
def __init__(self): #this is Default Constructor.
print("This is Default Constructor")
def __del__(self): #this is Destructor.
print("This is Destructor")
def fun(self): #this is simple function.
print("This is Simple Function")
obj = cls() #'obj' is object of class 'cls', and calling default constructor.
obj.fun() #calling 'fun()' function.
#when program Execution is finished then calling Destructor.
class mycls:
def __init__(self, arg): #this is parameterized Constructor.
print("This is "+ arg +" Constructor")
def fun(self): #this is Simple function.
print("This is Simple Function")
obj = mycls("Parameterized") #'obj' is object of class 'mycls' and calling
obj.fun() #and passing argument to parameter in Constructor.
class mycls: #'mycls' is name of Class.
def __init__(self, name, roll, sec): #this is parameterized constructor.
print("My Name is: ", name)
print("My Roll No is: ", roll)
print("My Section is: ", sec)
def fun(self, arg): #this is parameterized function.
print("This is "+ arg +" Function")
def fun2(self): #this is simple function.
print("This is Simple Function")
obj = mycls("HiLaLs Afridi", 12012, 'B') #calling and passing arguments,
obj.fun("Parameterized") #and 'ojb' is object.
obj.fun2()