0% found this document useful (0 votes)
6 views28 pages

Lecture6 - Constructors - Destructors in Python Class

This document provides an overview of constructors and destructors in Python, explaining their roles in object-oriented programming. It details the instantiation process, types of constructors, and compares Python's memory management with Java's garbage collection. Additionally, it includes exercises for students to reinforce their understanding of the concepts presented.

Uploaded by

Maringka Raissa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views28 pages

Lecture6 - Constructors - Destructors in Python Class

This document provides an overview of constructors and destructors in Python, explaining their roles in object-oriented programming. It details the instantiation process, types of constructors, and compares Python's memory management with Java's garbage collection. Additionally, it includes exercises for students to reinforce their understanding of the concepts presented.

Uploaded by

Maringka Raissa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Fakultas Ilmu Komputer

Learning is the process of acquiring new


understanding, knowledge, behaviors, skills,
values, attitudes, and preferences.

Constructors and Destructors


in Python Class
Welcome to the Object Oriented
Programming course
1. Sebutkan nama class dari
Python file di samping?

2. Sebutkan ada berapa


class variable?

3. Sebutkan nama method


di dalam class tersebut?

4. Sebutkan nama method


constructor?

5. Berapa parameter yang


terdapat di constructor?
1 - Class Constructors
Object Construction or Instantiation

Creating and initializing objects of a


given class is a fundamental step in
object-oriented programming. This step
is often referred to as object
construction or instantiation. The
tool responsible for running this
instantiation process is commonly
known as a class constructor.
What is Constructor in Python Class?
In object-oriented
programming, A constructor
(def __init__) is a special
method used to create and
initialize an object of a class.
This method is defined in the
class.

1) The constructor is executed automatically at the time of object creation.

2) The primary use of a constructor is to declare and initialize data


member/instance variables of a class.

3) The constructor contains a collection of statements (i.e., instructions) that executes


at the time of object creation to initialize the attributes of an object.
What is Destructor in Python Class?
In object-oriented programming, A destructor (def __del__) is called when an object
is deleted or destroyed. Destructor is used to perform the clean-up activity before
destroying the object, such as closing database connections or filehandle.
 Python has a garbage collector that
handles memory management
automatically.

 For example, it cleans up the memory when


an object goes out of scope.

 But it’s not just memory that has to be freed


when an object is destroyed. We must
release or close the other resources
object were using, such as open files,
database connections, cleaning up
the buffer or cache. To perform all those
cleanup tasks we use destructor in
Python.
Constructor vs Destructor
 The constructor is used to initialize objects, while the destructor is used to delete or destroy the
object that releases the resource occupied by the object.
 Constructor (konstruktor) digunakan untuk menginisialisasi objek, sedangkan destructor (destruktor)
digunakan untuk menghapus atau menghancurkan objek yang melepaskan sumber daya yang
ditempati objek tersebut.
Constructor vs Destructor in Python
Constructor vs Destructor
The destructor is the reverse (kebalikan) of the constructor.

Constructor Destructor
Constructor & Destructor in Java

The constructor is used to initialize objects, while the destructor is


used to delete or destroy the object that releases the resource occupied by
the object. Remember that there is no concept of destructor in Java. In
place of the destructor, Java provides the garbage collector that works
the same as the destructor.
Garbage Collection in Java
Garbage Collection is process of
reclaiming the runtime unused
memory automatically. In other words,
it is a way to destroy the unused
objects. The free() function is used in
C language and delete() in C++. But, in
Java it is performed automatically. So,
java provides better memory
management.

1) Garbage collection in Java is the process by which Java programs perform automatic
memory management.

2) Java programs compile (.java) to bytecode (.class) that can be run on a Java Virtual
Machine, or JVM for short.

3) When Java programs run on the JVM, objects are created on the heap, which is a portion
of memory dedicated to the program.

4) Eventually, some objects will no longer be needed.

5) The garbage collector finds these unused objects and deletes them to free up memory.
Java Virtual Machine (JVM)
Constructor & Destructor in PHP

Constructors: Constructors are called when an object is created from a class.


Destructors: Destructors are called when an object destructs.
Usually, it is when the script ends.
Constructor & Destructor in Python

The constructor is
used to initialize
objects, while the
destructor is used to
delete or destroy the
object that releases the
resource occupied by
the object.
Python’s Instantiation Process
When the class constructor
is called, which triggers

1) The instance creator,


.__new__(), to create a new
empty object.

2) The instance initializer,


.__init__(), which takes the
constructor’s arguments to
initialize the newly created
object.

3) The .__repr__() special method,


which provides a proper string
representation for the class.

Special method called __new__(), which is responsible for creating and returning a
new empty object. Then another special method, __init__(), takes the resulting
object, along with the class constructor’s arguments.
2 - Types of Constructors
Types of Constructors
In Python, there are three types of constructors:
1) Default Constructor
2) Non-parameterized constructor
3) Parameterized constructor
Default Constructor

Python will provide a default constructor if no constructor is defined.


Python adds a default constructor when we do not include the constructor
in the class or forget to declare it. It does not perform any task but initializes
the objects. It is an empty constructor without a body.
Non-parameterized Constructor

A constructor without any arguments is called a non-parameterized


constructor. This type of constructor is used to initialize each object with
default values. This constructor doesn’t accept the arguments during object
creation. Instead, it initializes every object with the same set of values.
Parameterized Constructor
A constructor with defined
parameters or arguments is
called a parameterized
constructor.

We can pass different values to


each object at the time of
creation using a parameterized
constructor.

The first parameter to


constructor is self that is a
reference to the being
constructed, and the rest of the
arguments are provided by the
A parameterized constructor programmer.
can have any number of
arguments.
Multiple Constructor in Python ?

Is it
possible to
define multiple
constructors in
Python?
Single Constructor with Default Parameters

It not is possible to define multiple constructors in Python. However, we can define


multiple default values (parameter values) if one is not passed.
Exercise for Students
Exercise #1 (Constructor & Destructor)
Exercise #2 (Instantiation Process)
Exercise #3 (Default Parameters)
END PRESENTATION
Thank you for your attention
Instructor: S – W – T

THANK YOU

You might also like