BinaryTree
Intermediate
Python Course
BinaryTree Digital Education Program
Week 4
Introduction to OOP
(Object-Oriented Programming)
Think About It
What is OOP?
Let's think about the Harry Potter series...
→ What is each book part of?
→ What does each book have (attributes)?
→ What are some actions in each book (methods)?
What is Object-Oriented Programming?
Open-Ended Question
Give a few reasons why developers use Object-Oriented Programming while building software.
⏱ 5 Minutes
Classes & Objects
Class
The Blueprint
Defines how objects should behave — like a recipe telling you how to make a dish.
Object
An Instance
An actual instance of a class, with its own unique data — like a specific dish (pasta or salad).
Many objects can be created from one class — code reuse with unique data per object.
Example — Classes & Objects
Based on this code, what would you expect as the output?
class Car:� def __init__(self, brand, color):� self.brand = brand� self.color = color�� def describe(self):� print(f"This is a {self.color} {self.brand}.")��my_car = Car("Toyota", "red")�my_car.describe()
Instance Variables and Methods
Instance Variables
Store unique data for each object (e.g., a car's color or brand). Created inside __init__.
Instance Methods
Functions inside a class that allow objects to perform actions — drive, brake, honk.
Together, they let objects store information and interact — making programs more dynamic.
The Concept of "self" in Python
1
self represents the current instance of the class.
2
Each object maintains its own separate data — self ensures the correct object's attributes are used.
3
Without self, Python couldn't differentiate between different objects.
def greet(self): → self.name is this object's name, not some other object's
Constructors and Class Attributes
Constructor (__init__)
Automatically called when an object is created. Initializes instance variables.
def __init__(self, name):
self.name = name
Class Attributes
Belong to the class itself — all instances share the same value unless overridden.
species = "Mammal" # shared by all
Instance Attributes
Unique to each object. Set via self inside __init__.
self.name = name # unique per object
Optional Parameters
Some class parameters have preset (default) values. If not provided, the default is used.
class Car:� def __init__(self, brand="Unknown", color="White"):� self.brand = brand� self.color = color��car1 = Car("Ferrari", "Red") # all params provided�car2 = Car("Bugatti") # color defaults to White�car3 = Car() # both default�
Reference Variables — Scope
Type | Where Defined | Scope | Access |
Method Variable | Inside a function/method | Local — exists only within the function | Cannot be accessed outside |
Global Variable | Outside all functions | Global — accessible throughout the program | Use carefully — avoid naming conflicts |
Instance Variable (self.) | Inside __init__ | Object-level — unique per instance | Access via self.variable_name |
Open-Ended Question
Differentiate between method variables and global variables.
→ When would you use each?
→ What happens if they share the same name inside a class?
⏱ 5 Minutes
Clean Code vs. Spaghetti Code
Clean Code
Well-organized, structured, follows OOP principles.
• Low coupling between classes
• Easy to read and debug
• Efficient and maintainable
Spaghetti Code
Unstructured, tightly coupled, hard to follow.
• Leads to harder-to-catch bugs
• Performance issues
• Term coined to describe "tangled" logic
Always aim for clean, organized OOP — it makes your code easier for others (and your future self) to understand.
Practice Question
What do class attributes define?
A
Which syntax to use for a certain scenario.
B
Shared properties across all objects in a class.
C
Parameters to use when defining objects.
D
Variables to assign to values in your code.
Practice Question
What is unstructured, inefficient, and unorganized code called?
A
Pasta code
B
Salad code
C
Spaghetti code
D
Cereal code
Practice Question
What would print(dog.species + " " + dog.name) output if species = "Mammal" and name = "Python"?
A
C++ Python
B
C++ Mammal
C
species Python
D
Mammal Python
Practice Question
How are instance variables created?
A
By using the constructor __init__.
B
By running print(instance_variable).
C
By using only self.
D
By using swapping logic with a placeholder variable.
Practice Question
In Python, what is the purpose of "self"?
A
To show the user's coding software (IDE).
B
To represent the current instance of a class.
C
To represent the current instance of an object.
D
To represent the current instance of a method variable.