0% found this document useful (0 votes)
3 views18 pages

OOP in Python

The document provides an overview of Object-Oriented Programming (OOP) in Python, explaining key concepts such as classes, objects, attributes, and methods. It highlights the advantages of OOP, including code organization, reusability, and maintenance. Additionally, it covers principles like inheritance, polymorphism, encapsulation, and abstraction, along with practical examples of implementing these concepts in Python.

Uploaded by

eliot nightray
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)
3 views18 pages

OOP in Python

The document provides an overview of Object-Oriented Programming (OOP) in Python, explaining key concepts such as classes, objects, attributes, and methods. It highlights the advantages of OOP, including code organization, reusability, and maintenance. Additionally, it covers principles like inheritance, polymorphism, encapsulation, and abstraction, along with practical examples of implementing these concepts in Python.

Uploaded by

eliot nightray
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

Python OOP

An enhancement of Python basic from EIU STEC


What is OOP?
OOP stands for Object-Oriented Programming.
Python is an object-oriented language, allowing user to structure code using classes
and objects for better organization and reuseability.

Advantages of OOP:
Provides a clear structure to programs
Makes code easier to maintain, reuse, and debug
Helps keep your code DRY (Don't Repeat Yourself)
Allows you to build reusable applications with less code

2
A class is the blueprint or template for
Class - Lớp
creating objects

Object - Đối tượng An object is a concrete instance created


from a class

Attribute - Thuộc tính The attribute is a data variable inside an


objec or a class
A method is a function defined inside a
Method - Phương thức
class that describes the behavior of the
object
Constructor - Hàm khởi tạo
A constructor is a special method of a class
that runs automatically when an object is
created 3
Object - Đối tượng
Class - Lớp
A person name
Design of a person Nguyen Van A

Attribute - Thuộc tính Method - Phương thức


Gene
Body parts
Gender
Age
Nationality Eat - Sleep - Talk - Breath....
...
4
Classes and Objects

5
Class - Lớp & Object - Đối tượng
To create a class, use the keyword: class

class easyMath (): Intialize a class with name of “easyMath”, and


result = 0 and has 01 class attribute name of “result”

A object name cal_easyMath that can use


cal_easyMath = easyMath()
attributes and methods in easyMath

print(cal_easyMath.result) Use “result" attribute in easyMath

Multiple objects from the easyMath class


6
Class attribute and Instance attribute

“result” is a class attribute of easyMath

[Link], [Link], and


[Link] have the same result,
that is 0 (defined in easyMath)

the objects do not have their own data yet; they all share the same class attribute “result”.

7
Class attribute and Instance attribute

Python __init__() method


All classes have a built-in method called __init__( ), which is always executed when the
class is being initiated.
__init__ ( ) method is used to assign values to object properties, or to perform operations
that are necessary when the object is being created.

class easyMath():
def __init__(self):
[Link] = 0
print("This is easyMath class")
easyMath()

8
Class attribute and Instance attribute
self parameter
The self parameter is a reference to the current instance of the class.
It is used to access properties and methods that belong to the class.

class easyMath():
def __init__(self):
[Link] = 0
print("This is easyMath class")
ex1 = easyMath()
ex2 = easyMath()
[Link] = 1
[Link] = 2
Using the self parameter, each object can
print(easyMath().result)
print([Link]) gets its own value for the attribute.
print([Link]) “result” turn into instance attribute 9
Challenge

Write 4 functions that are used to add, multiply,


divide and substract.

10
Class Methods
Methods are functions that belong to a class.
They define the behavior of objects created from the class.
class easyMath():
def __init__(self):
[Link] = 0
def cal_mul(self,a,b):
[Link]=a*b
return [Link]
def cal_div(self,a,b):
[Link] = a/b
return [Link]
uyen = easyMath()
print(uyen.cal_div(10,5))
11
Basic properties of OOP
Inheritance - Tính kế thừa

Polymorphism - Tính đa hình

Encapsulation - Tính đóng gói

Abstraction - Tính trừu tượng


12
Python Inheritance
Inheritance allowe users to define a class that inherits all the methods and properties
from another class.

Parent class Child class

class EasyMath: class extraMath (EasyMath):


def __init__(self): pass
[Link] = 0 try to call child class then print “result”
attribute, and “class_att” attribute
def add(self, a,b):
[Link] = a+b extraMath( )
def sub(self, a,b): print([Link])
[Link] = a-b object_example = extraMath()
print(object_example.result)
13
Python Inheritance
A subclass can override and extend the methods of its parent class by calling the parent
implementation using super().

Parent class Child class Example


class EasyMath: class extraMath(EasyMath): inherit_ex = extraMath()
class_att = 0 def __init__(self): inherit_ex.sub(3,1)
def __init__(self): super().__init__() print(inherit_ex.result)
[Link] = 0 def mul(self,a,b):
def add(self, a,b): [Link] = a*b object inherit_ex call sub
[Link] = a+b def div(self,a,b): method from extraMath class
def sub(self, a,b): [Link] = a/b
[Link] = a-b

14
Python Polymorphism

Multiple classes with the same method name

MOVE

Walk/run/... Drive Fly Swim

15
Python Polymorphism

Multiple classes with the same method name

Parent class Child class Example


class easyMath: class polymor_ex =
def __init__(self): extraMath(easyMath): [extraMath(),easyMath()]
[Link] = 0 def __init__(self): for i in polymor_ex:
def add(self, a,b): [Link](5,3)
super().__init__()
print([Link])
[Link] = a+b
def sub(self, a,b): def mul(self,a,b): each class has its own
[Link] = a-b [Link] = a*b divided method, base on how
def div(self,a,b): def div(self,a,b): user apply the method
[Link] = a//b [Link] = a/b
16
Python Encapsulation
Encapsulation is about protecting data inside a class.
It means keeping data (properties) and methods together in a class, while controlling how the
data can be accessed from outside the class.
This prevents accidental changes to your data and hides the internal details of how your class
works.

Private Public
17
Python Encapsulation
self._var → do not touch or change data in this attribute
self.__var → can not access outside the class, except using the specific way

18

You might also like