1. Introduction: Implementing OOP in Python
In previous modules, we examined the theoretical concepts, benefits, and foundations of Object-Oriented Programming (OOP). In this topic, we transition from theoretical concepts to practical implementation in Python using Jupyter Notebook by creating and practicing with our first custom Class and Object.
Before generating any physical instance or object, Python requires the creation of a Class blueprint.
2. Step 1: Building a Minimal Class Template
To define a new class, use the class keyword, followed by the class name, and terminate the header with a colon (:). Indented underneath with a tab or four spaces, define its first method using the def keyword:
# 1. Defining the Car class blueprint
class Car:
def drive(self):
print("The car is moving")
How Python Interprets the Blueprint
- The
class keyword informs Python that a new class template is being defined.
Car names the template.
def drive(self): declares a method named drive inside the class.
- The
self parameter serves as the object reference, allowing Python to know which specific object instance called the method.
- The method body contains an executable statement,
print("The car is moving").
When you execute this code cell in Jupyter Notebook, no output is displayed. This occurs because you have only constructed the blueprint or design. An architectural design cannot drive on the road; output and actions only occur once an actual object is instantiated from the template.
3. Step 2: Instantiating an Object and Calling Methods
To create a real object based on the Car template, assign the class constructor call to a variable:
# 1. Creating the first car object instance
car1 = Car()
# 2. Calling the drive method on car1
car1.drive()
Execution Walkthrough
- Writing
car1 = Car() instructs Python to construct an object named car1 using the Car template.
- As a result, all data attributes and methods defined inside
Car become available inside car1.
- Calling
car1.drive() uses parentheses to invoke the method.
Why We Do Not Manually Pass self
Notice that when calling car1.drive(), no argument is placed inside the parentheses. self is an automatic input parameter. When you write car1.drive(), Python automatically passes the calling object (car1) into the self parameter behind the scenes. Therefore, you do not pass self manually.
Output:
The car is moving
4. Step 3: Adding Data Attributes to the Class
A complete object contains both functions (methods) and variables (data attributes). We can expand the Car class by adding a data attribute named color with a default value of "red", and updating the drive method to reference that attribute:
class Car:
# Adding a data attribute with a default value
color = "red"
def drive(self):
print(f"{self.color} car is moving")
Testing the Updated Blueprint
car1 = Car()
car1.drive()
Output:
red car is moving
- By accessing
self.color, the drive method prints the specific color associated with that object.
- If the car's color is red, it outputs "red car is moving"; if green, it outputs "green car is moving".
5. Step 4: Modifying Attributes with Setter Methods
In Object-Oriented Programming, objects contain data attributes and methods designed to operate on or modify those attributes. To change the default color of a car, we can add a method called set_color:
class Car:
color = "red"
def drive(self):
print(f"{self.color} car is moving")
# Setter method to update the car's color
def set_color(self, new_color):
self.color = new_color
Understanding the Setter Method
- The
set_color method accepts self (passed automatically) along with a second parameter: new_color.
- Inside the method body,
self.color = new_color reassigns the object's color attribute to the new value.
- Methods dedicated to updating or assigning values to attributes are commonly called setters.
6. Step 5: Instantiating Multiple Independent Objects
One of the core strengths of OOP is that you can instantiate as many independent objects as needed from a single class template, each maintaining its own distinct attributes:
# 1. Instantiate a second, distinct car object
car2 = Car()
# 2. Modify the color of car2 using our setter method
car2.set_color("green")
# 3. Call the drive method on car2
car2.drive()
Output:
green car is moving
Key Takeaways
- Calling
car2.set_color("green") updates the internal color of car2 to "green".
- When
car2.drive() executes, it prints "green car is moving" instead of "red car is moving".
- The template remains reusable and scalable: developers can add more attributes and methods, building up complexity while creating distinct objects with unique data states.