Object Oriented Programming: A Beginner’s Python Guide

Have you ever felt overwhelmed writing Python code as your projects grow? Do you find yourself creating countless disconnected variables and functions that become difficult to manage? If you’ve been writing scripts in a linear, function-by-function style, you’ve been using procedural programming. While it works for small tasks, there’s a better way to build complex applications.

This guide will introduce you to Object Oriented Programming (OOP), the powerful programming paradigm that transforms how you structure your code. By the end of this post, you’ll understand the core concepts of OOP, why it’s essential for modern software development, and how it helps you think about code in a more intuitive, human-friendly way.

What is Object Oriented Programming?

Object Oriented Programming (OOP) is a programming methodology that organizes code around “objects” rather than functions and logic. An object is a self-contained unit that contains both data (known as attributes or properties) and behaviors (known as methods) that operate on that data.

Think of it like this: in the real world, you interact with objects all the time. Your phone is an object. It has properties like its color, model, and RAM. It also has behaviors like making calls, taking photos, and connecting to WiFi. OOP brings this natural way of thinking into programming.

The Problem with Functional/Procedural Programming

Before we dive deeper into OOP, let’s understand the problem it solves. So far, you’ve likely been using functional or procedural programming. You break down a task into a sequence of steps and write functions to execute those steps.

For example, imagine building a simple website like Amazon. How would you manage a product, say a book, using procedural programming?

# Procedural Approach - Creating separate variables for each property
book1_name = "Automatic Habits"
book1_num_of_pages = 304

book2_name = "Four Day Work Week"
book2_num_of_pages = 200

# What about a mobile phone?
mobile1_name = "SmartPhone X"
mobile1_ram = "6GB"

This approach has several major drawbacks:

  1. No Standard Naming: One programmer might use book2_num_of_pages, while another uses b3_no_pg. This inconsistency makes code hard to read and maintain.
  2. Difficult to Scale: Imagine creating thousands of variables for thousands of products. It quickly becomes unmanageable.
  3. No Security/Restrictions: Functions that operate on data are separate. A return_item() function could be mistakenly called with any variable, even one it wasn’t designed for, like trying to “return” a digital song that isn’t returnable.

The OOP Solution: Thinking in Objects

OOP solves these problems by bundling related data and functions together. Let’s revisit our Amazon example. What are the fundamental “things” or objects on the site?

  • Products (like Book and Mobile)
  • Shopping Cart
  • User

Each of these can be modeled as an object. A Book object would bundle all its properties and behaviors into one place.

Real-World OOP Analogy: A Snake Game

The video provides a brilliant analogy with a classic Snake game. Let’s break it down in OOP terms:

  • The Snake is an Object:
    • Properties: length, position on the board, current direction.
    • Behaviors: move(), eat_apple(), collide_with_wall().
  • The Apple is an Object:
    • Properties: position on the board, points_value.
    • Behaviors: appear_at_new_location().
  • The Game Board is an Object:
    • Properties: score, high_score, grid_size.
    • Behaviors: update_score(), draw().

This is how humans naturally think about the game! We don’t think about a global move() function; we think “the snake moves.” OOP allows us to translate this mental model directly into code.

Core OOP Concepts in Action: A Python Example

Let’s translate the “Snake” idea into actual Python code. In OOP, we first define a class, which is a blueprint for creating objects.

Here’s a simple Snake class:

# Defining a Class (Blueprint)
class Snake:
    # The __init__ method is a special constructor method
    # It initializes the object's properties (attributes)
    def __init__(self, starting_length):
        self.length = starting_length  # Property
        self.position = [0, 0]         # Property (x, y coordinates)
        self.direction = "RIGHT"       # Property

    # These are methods (behaviors) of the Snake class
    def move(self):
        """Simulates the snake moving one step in its current direction."""
        if self.direction == "RIGHT":
            self.position[0] += 1
        # ... (logic for other directions)
        print(f"Slithering to {self.position}")

    def eat(self):
        """Increases the snake's length when it eats."""
        self.length += 1
        print(f"Yum! Snake grew to {self.length} segments.")

    def collide(self):
        """Ends the game if the snake collides."""
        print("Game Over! The snake collided.")

# Creating Objects (Instances) from the Class
snake1 = Snake(starting_length=1)  # snake1 is an 'instance' of Snake
snake2 = Snake(starting_length=3)  # snake2 is another, separate instance

# Using the objects and their methods
print(snake1.length)  # Output: 1
print(snake2.length)  # Output: 3

snake1.move()         # Output: Slithering to [1, 0]
snake1.eat()          # Output: Yum! Snake grew to 2 segments.
print(snake1.length)  # Output: 2 (see, it changed!)

This example shows the power of OOP:

  • Encapsulation: The data (lengthposition) and the behaviors (move()eat()) are bundled together inside the Snake class.
  • Reusability: We can create multiple Snake objects (snake1snake2) from the same blueprint, each with its own independent state.

Let’s also create a simple Apple class to see how they interact.

class Apple:
    def __init__(self):
        self.position = [5, 5]

    def get_eaten(self, snake):
        """Handles logic when the apple is eaten by a snake."""
        print("Apple was eaten!")
        snake.eat()  # We can call the snake's method from here!
        self.appear_at_new_location()

    def appear_at_new_location(self):
        """Moves the apple to a new random position."""
        # Simplified logic for the example
        self.position[0] += 2
        self.position[1] += 2
        print(f"Apple reappeared at {self.position}")

# Creating objects and making them interact
player_snake = Snake(1)
red_apple = Apple()

red_apple.get_eaten(player_snake)
# Output:
# Apple was eaten!
# Yum! Snake grew to 2 segments.
# Apple reappeared at [7, 7]

Ready to Go from Basics to Pro?

Understanding OOP is a major milestone, but truly mastering it—along with all the other essential Python concepts—requires a structured path. It’s one thing to follow a tutorial, and another to confidently build your own projects from scratch. A comprehensive course provides the guided practice, expert code reviews, and real-world projects that transform knowledge into skill.

If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.

Conclusion

Object Oriented Programming is a fundamental shift from thinking in steps to thinking in objects. It helps you write code that is:

  • More Organized: Related data and functions are bundled together.
  • Easier to Maintain: Code is modular, so you can fix or improve one part without breaking others.
  • Easier to Scale: Perfect for building large, complex applications like games, web apps, and data analysis tools.
  • More Intuitive: It models the real world, making it easier to design and reason about your software.

Keep practicing by identifying objects in the software you use every day. This “object-thinking” is the first and most crucial step to becoming proficient in OOP.

Common Questions About Object Oriented Programming

What are the four main principles of OOP?
The four pillars are: 1) Encapsulation: Bundling data and methods into a single unit (the class). 2) Abstraction: Hiding complex implementation details and showing only essential features. 3) Inheritance: Allowing a new class to inherit properties and methods from an existing class. 4) Polymorphism: Allowing objects of different classes to be treated as objects of a common super class.

Is OOP necessary for every Python project?
No, for simple scripts or data analysis tasks, procedural programming might be sufficient and faster to write. However, for any medium to large-scale application, OOP is highly recommended and often essential for managing complexity.

What’s the difference between a class and an object?
class is the blueprint (e.g., the architectural plans for a house). An object is an instance of that class (e.g., an actual house built from those plans). You use the blueprint to create as many individual, unique houses (objects) as you want.