Python Tutorial
Python - Introduction Python - Syntax Python - Variable Python - Number Python - Casting Python - String Python - Operators Python - Conditions Python - Loops Python - List Python - Tuple Python - Set Python - Dictionary Python - Function Python - Module Python - Date & Time Python - Input Output Python - Error & Exception Python - File Handling
Python OOP
OOP - Introduction OOP - Class Members OOP - Constructor Destructor OOP - Data Hiding OOP - Inheritance OOP - Overriding OOP - Overloading

Python Constructor and Destructor

Python Constructor

Constructor is a special method that executes when we create an object of a class.

Constructor is defined using __init__() keyword:


Python Destructor

Destructors are called when an object gets destroyed. It’s the polar opposite of the constructor, which gets called on creation.

Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.

Destructor is defined using __del__() keyword:


Python Garbage Collection

Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space.

The process by which Python periodically reclaims blocks of memory that no longer are in use is termed as Garbage Collection.

Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero.

An object's reference count changes as the number of aliases that point to it changes.