Skip to main content

OOP: Classes and Objects

Organizing Code with Blueprints

· 3 min read

In the previous unit, we learned about file operations. Now let's explore Object-Oriented Programming (OOP), a way of organizing code around objects that represent real-world entities.

OOP: Classes and Objects

What Is OOP?

Object-Oriented Programming structures code using objects. Objects have attributes (data) and methods (behaviors). Classes are blueprints that define what attributes and methods objects of that type will have.

Think of a class as a cookie cutter and objects as the cookies. The class defines the shape, and each object is a specific instance with its own values.

Defining a Class

Here's a simple class definition:

class Shape:
def __init__(self, sides, color):
self.sides = sides
self.color = color

def describe(self):
print(f"A {self.color} shape with {self.sides} sides.")

The class keyword starts the definition. Shape is the class name. Inside, we define __init__, a special method called when creating an object. It initializes the object's attributes. The self parameter refers to the object being created.

The describe method is a regular method that can access the object's attributes through self.

Creating Objects

Create an object by calling the class like a function:

my_shape = Shape(4, "red")
my_shape.describe() # Outputs: A red shape with 4 sides.

The arguments 4 and "red" get passed to __init__, which stores them in self.sides and self.color. Each object can have different values for these attributes.

Instance Variables and Methods

Instance variables belong to a specific object. In our Shape class, sides and color are instance variables, so each shape object has its own values.

Instance methods are functions defined in a class that operate on the object's data. They always take self as their first parameter, giving them access to the object's attributes.

Project: Shape Class with Turtle

Let's create a Shape class that uses Turtle to draw itself:

import turtle

class Shape:
def __init__(self, t, sides, length):
self.t = t
self.sides = sides
self.length = length

def draw(self):
for _ in range(self.sides):
self.t.forward(self.length)
self.t.right(360 / self.sides)

screen = turtle.Screen()
screen.setup(width=800, height=600)
screen.bgcolor("white")

t = turtle.Turtle()

# Create shape objects
square = Shape(t, 4, 100)
pentagon = Shape(t, 5, 70)

# Draw the square
square.draw()

# Move to a new position
t.penup()
t.goto(150, 0)
t.pendown()

# Draw the pentagon
pentagon.draw()

t.hideturtle()
turtle.done()

The Shape class stores the turtle, number of sides, and side length. The draw method loops through the sides, moving forward and turning by the appropriate angle.

We create two objects: square with 4 sides of length 100, and pentagon with 5 sides of length 70. Each object uses the same draw method but produces different shapes based on its attribute values.

This is the power of OOP: define behavior once in the class, then create objects with different configurations.

In the next unit, we'll explore more about methods and class variables in OOP.