0% found this document useful (0 votes)
4 views14 pages

Topic 2 OOP Python Design Principles

This document outlines the basic principles of Object-Oriented Programming (OOP) in Python, including encapsulation, information hiding, separation of concerns, reusability, and maintainability. It emphasizes the importance of organizing code effectively and ensuring that each class has a single responsibility. Good OOP design leads to safer, more organized, and reusable code in Python applications.

Uploaded by

k a i ..
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)
4 views14 pages

Topic 2 OOP Python Design Principles

This document outlines the basic principles of Object-Oriented Programming (OOP) in Python, including encapsulation, information hiding, separation of concerns, reusability, and maintainability. It emphasizes the importance of organizing code effectively and ensuring that each class has a single responsibility. Good OOP design leads to safer, more organized, and reusable code in Python applications.

Uploaded by

k a i ..
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

OOP Python

Design Principles
At the end of the lesson, the students should
be able to:
• Explain the basic principles of Object-
Intended Oriented Programming (OOP) design
Learning • Apply proper class design in Python
Outcomes • Demonstrate code reusability to minimize
repetition
• Develop Python applications that are easy
to read, fix and improve
Basic Principles of
OOP Design
OOP design is a way of writing programs by
grouping data and actions together using
objects.

Think of a real-world object, like a car:


• Data/Characteristics → color, model, speed
• Actions → start, stop, accelerate

In Python:
• Class = blueprint
• Object = actual/real thing made from the
blueprint
• Method = action that the object can do
Basic Principles of OOP Design - Example

class Car: How the Program Runs:


def start(self):
[Link] reads the Car
print("Car is starting") class
[Link] creates an
my_car = Car() object called my_car
my_car.start() 3.my_car.start() is called
[Link] prints the
Output: message
Car is starting

In OOP, we create classes as blueprints and objects as real-world things that can perform actions.
Encapsulation = bundling data and methods together in a class.
• Think of it as putting all related things in one box.
Encapsulation and
Information Hiding
• It organizes your code, so data and actions belong together.

Example:
class Car:
def __init__(self, speed):
[Link] = speed # data
def drive(self):
print("Driving at", [Link], "km/h") # method

Here, speed (data) and drive() (method) are bundled inside the
Car class → that’s encapsulation.
Information hiding = restricting direct access to some data.
• You hide the details from outside code, so it can only interact safely through methods.
• Usually done with private variables (__variable) in Python.
Encapsulation and
Information Hiding

Example:
class Car:
def __init__(self, speed):
self.__speed = speed # hidden data

def get_speed(self):
return self.__speed

my_car = Car(120) # Create an object of Car


print("The speed of the car is:", my_car.get_speed()) # Access the hidden variable using the
method

• __speed is hidden (private variable), so you cannot do my_car.__speed directly.


• You use the getter method get_speed() to read its value safely.
• This protects the data and ensures safe access → that’s information hiding.
Example#2
with
Information
Hiding
Separation of
Concerns (SOC)
Separation of concerns means each
class or function has only ONE job or
responsibility.

ANALOGY: Imagine a restaurant:


• Chef → cooks
• Cashier → takes payment
• Waiter → serves food
So, if one person does everything → messy
and confusing
Bad Example Good Example
one class doing too many things separate responsibilities
class Student: class Student:
def __init__(self, name): def __init__(self, name):
[Link] = name [Link] = name
def display(self):
class StudentDisplay:
print([Link])
def show(self, student):
def save(self): print([Link]) Why this is good?
print("Saving student data...") Easier to understand
# Create a Student object Easier to fix errors/test
# Create a Student object student2 = Student(“Ces") Easier to update/modify
student1 = Student("Alice")
# Create a StudentDisplay object
# Display name display = StudentDisplay()
[Link]()

# Save student data # Show the student name


[Link]() [Link](student2)
The Student class is handling Now the Student class only holds data, and
both display and saving, which StudentDisplay handles showing it. Each
mixes responsibilities. class has one clear job.
Code Reusability &
Maintainability
• Reusability → Write code once, use it many
times
• Maintainability → Easy to fix, update, or extend
later

ANALOGY:
Think of LEGO blocks
• One block
• Many structures
Reuse blocks instead of rebuilding.
class Person:
Example def greet(self):
print("Hello!")
(Reusability class Teacher(Person):
Using pass Why this is good?
No repeated code or less code
Inheritance) class Student(Person):
pass
duplication
Easy to add new roles
Easy to maintain
t = Teacher()
s = Student()
[Link]()
[Link]()
class User:
def __init__(self, username):
self.__username = username # encapsulation

Maintainable def get_username(self):


return self.__username
Code Example
(Clean class UserDisplay:
Structure) def show(self, user): # separation of concerns
print("Username:", user.get_username())

user = User("Ces")
display = UserDisplay()
[Link](user)

This example shows how to protect data, organize code and make it easy to change.
One class stores info, one class shows info and they work together safely.
That’s good OOP design and maintainable code.
Applying Design Principles in Real Python Programs
# Encapsulation & Information Hiding
class Student:
def __init__(self, name):
self.__name = name # hidden data

def get_name(self): What this example shows?


return self.__name  class Student → blueprint for a student
 self.__name → data is hidden (information hiding)
 get_name() → allows safe access to the hidden data
# Separation of Concerns  class StudentDisplay → only shows student info
class StudentDisplay: (separation of concerns)
def show(self, student):  [Link](student) → reusable display method,
keeps code clean (maintainable)
print("Student Name:", student.get_name())

# Usage
student = Student("Ana")
display = StudentDisplay()
[Link](student)
Summary
Encapsulation: Put data and methods
together
Information hiding: Protect data from direct
access
Separation of concerns: One class, one job
Reusability: Avoid repeating code
Maintainability: Easy to read, fix and
improve
Good OOP design makes code safe, organized
and reusable.

You might also like