2023 Slot08 OOPUsingPython
2023 Slot08 OOPUsingPython
Slot 08 –
OOP Using Python
Advisor:
Dr. Nguyễn Tiến Huy
Dr. Lê Thanh Tùng
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 1
Content
1 Introduction
2 Terminologies
3 OOP Concepts
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 2
Introduction
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 3
Introduction
Two basic programming paradigms:
▪ Procedural:
▪ Organizing programs around functions
or blocks of statements which
manipulate data.
▪ Object-Oriented
▪ combining data and functionality and
wrap it inside what is called an object
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 4
Introduction
▪ Object-oriented
programming (OOP) is a
computer programming
model that organizes
software design around
data, or objects, rather
than functions and logic
▪ An object can be defined as a data field that has unique
attributes and behavior
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 5
Introduction
Object-Oriented (OOP) Vs Procedural Oriented (POP) Programming
OOP POP
It is a bottom-up approach It is a top-down approach
Program is divided into objects Program is divided into functions
Makes use of Access modifiers
Doesn’t use Access modifiers
“public”, “private”, “protected”
It is more secure It is less secure
Object can move freely within Data can move freely from function
member functions to function within programs
It supports inheritance It does not support inheritance
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 6
Terminologies
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 7
Object and Class
▪ A class is a collection of objects,
or you can say it is a blueprint of
objects defining the common
attributes (data) and behavior
(methods)
▪ A class is a mechanism used to
create new user-defined data
structures
▪ Objects are instances of the class
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 8
Object and Class
▪ For example, think about the class of car:
▪ What are the attributes of car?
▪ What are the methods of car?
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 9
Object and Class
▪ In python, we can create a class using the keyword class
▪ Method of class can be defined by keyword def
▪ The first parameter in the definition of a method is always self and
method is called without the parameter self
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 10
Object and Class
▪ Create a class in Python
class Car:
# attributes
color = "red"
price = 1500
model = "CX-5"
# methods
def drive(self):
pass
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 11
Object and Class
▪ How about the object
▪ It is the specific instant from the class
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 12
Constructor
▪ Constructor is a special method
▪ You can think of as a function which
initializes or activates the attributes or
properties of the class for a object
▪ For example, you can think of
constructor as entire sequence of
actions required so that the factory
constructs a car object out of the class
design pattern
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 13
Constructor
▪ Objects are instances of a class
▪ Words “instance” and “object” are used
interchangeably
▪ The process of creating an object of a class is called instantiation
▪ In the following example, we are asking user to input values as the initial
attributes of the specific car
▪ The methods __init__ is called when ever an object of the class is
constructed
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 14
Constructor
class Car:
# attributes
__color = "red"
__price = 1500
__model = "CX-5"
# Constructor
def __init__(self, color, price, model):
self.__model = model
self.__color = color
self.__price = price
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 16
Some points on Python class
▪ Classes are created by keyword class.
▪ Attributes are the variables that belong to a class.
▪ Attributes are always public and can be accessed using the dot (.)
operator
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 17
Some points on Python Object
An object consists of
▪ State: It is represented by the attributes of an object. It also reflects the
properties of an object.
▪ Behavior: It is represented by the methods of an object. It also reflects
the response of an object to other objects.
▪ Identity: It gives a unique name to an object and enables one object to
interact with other objects.
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 18
Call a method
▪ An Object will call a method to modify its attribute and affect its work
via dot operator: .<name of method>
class Car:
# ...
def update_price(self, new_price):
self.__price = new_price
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 19
OOP Concepts
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 20
OOP Concepts
▪ Class in Python
▪ Objects in Python
▪ Polymorphism in Python
▪ Encapsulation in Python
▪ Inheritance in Python
▪ Data Abstraction in Python
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 21
OOP Concepts
▪ Class in Python
▪ Objects in Python
▪ Polymorphism in Python
▪ Encapsulation in Python
▪ Inheritance in Python
▪ Data Abstraction in Python
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 22
THANK YOU
for YOUR ATTENTION
Dr. LE Thanh Tung MTH083 - Advanced Programming for Artificial Intelligence Page 23