Q:

Python | Example to implement destructor and constructors using __del__() and __init__()

belongs to collection: Python class & object programs

0

To implement a constructor, we use __init()__ and to implement a destructor, we use __del()__ in python.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

class Employee:
    def __init__(self): #Constructor
        self.__id = 0
        self.__name = ""
        self.__gender = ""
        self.__city = ""
        self.__salary = 0
        print("Object Initialized.")
    def __del__(self): #Destructor
        print("Object Destroyed.")
    def setData(self):
        self.__id=int(input("Enter Id\t:"))
        self.__name = input("Enter Name\t:")
        self.__gender = input("Enter Gender:")
        self.__city = input("Enter City\t:")
        self.__salary = int(input("Enter Salary:"))
    def showData(self):
        print("Id\t\t:",self.__id)
        print("Name\t:", self.__name)
        print("Gender\t:", self.__gender)
        print("City\t:", self.__city)
        print("Salary\t:", self.__salary)


def main():
    #Employee Object
    emp=Employee()
    #emp.setData()
    emp.showData()

if __name__=="__main__":
    main()

Output

 
Object Initialized.
Id              : 0
Name    :
Gender  :
City    :
Salary  : 0
Object Destroyed.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Example to implement Getters and Setters ... >>
<< Python program to calculate student grade...