Skip to main content

Core OOP Concepts: Classes and Objects

Welcome to a new chapter in your Python journey! So far, we've been using a style of programming called procedural programming. We write procedures (functions) that perform operations on data. This is great, but as programs become larger and more complex, it can be hard to manage the relationship between the data and the functions that operate on it.

Object-Oriented Programming (OOP) is a powerful programming paradigm designed to solve this problem. It focuses on creating "objects" that bundle together both data (attributes) and the functions that operate on that data (methods). This article introduces the two most fundamental concepts in OOP: classes and objects.


📚 Prerequisites

A solid understanding of Python's basic data types (strings, numbers, lists) and functions is required.


🎯 Article Outline: What You'll Master

In this article, you will learn:

  • The "Why" of OOP: Understand the core idea of bundling data and behavior together.
  • Classes as Blueprints: Learn that a class is a template for creating objects.
  • Objects as Instances: Understand that an object is a concrete instance created from a class blueprint.
  • The class Keyword: How to define a new class in Python.
  • The __init__() Method: How to use the special constructor method to initialize an object's data.
  • Creating Your First Object: How to instantiate an object from your custom class.

🧠 Section 1: The Core Idea - Blueprints and Instances

Imagine you are an architect. You want to build a house. First, you draw up a detailed blueprint. This blueprint isn't a house itself; it's the plan for a house. It defines all the essential properties: how many rooms, the size of the windows, the type of roof, etc.

In OOP, a Class is like that blueprint. It's a template that defines the properties and behaviors that all objects of a certain type will have.

Now, using that single blueprint, you can build multiple, actual houses. Each house you build is a real, physical object. They all share the same structure from the blueprint, but each one is a unique instance. One might be painted blue, another red. One might have a garden, another might not.

In OOP, an Object is like one of those houses. It's a specific, concrete instance of a class.

In short:

  • Class: The blueprint, the template, the recipe.
  • Object: The actual thing you create from the blueprint.

💻 Section 2: Defining a Class

To define a class in Python, you use the class keyword, followed by the name of the class. By convention, class names in Python use PascalCase (also known as CapitalizedWords).

Let's create a blueprint for a Dog.

# simple_class.py

class Dog:
pass # The 'pass' keyword means we're not adding any properties or behaviors yet.

We've just defined a class named Dog. It's a very simple blueprint that doesn't contain any information yet, but it's a valid class.


🛠️ Section 3: Initializing an Object with __init__()

Our Dog blueprint is currently empty. How do we specify that every dog should have a name and an age when it's created? We use a special method called __init__().

This method is the constructor. It's automatically called every time you create a new object from the class. Its job is to set up the initial state and attributes of the object.

The __init__ method's first parameter is always named self by convention. self refers to the specific instance of the object being created.

# class_with_init.py

class Dog:
# This is the constructor method
def __init__(self, name: str, age: int):
# 'self' refers to the specific Dog object being created.
# We are creating attributes on the object and assigning them values.
self.name = name
self.age = age
print(f"A new dog named '{self.name}' has been created!")

Code Breakdown:

  1. def __init__(self, name, age): defines the constructor. self is always the first parameter, followed by any other parameters needed to create the object.
  2. self.name = name: This line creates an attribute called name on the Dog object and assigns it the value of the name parameter that was passed in.
  3. self.age = age: This does the same for the age.

These variables (self.name, self.age) are called instance attributes because they belong to a specific instance (object) of the class.


🚀 Section 4: Creating an Object (Instantiation)

Now that we have our Dog blueprint, we can create actual Dog objects. The process of creating an object from a class is called instantiation.

You do this by calling the class name as if it were a function, passing in the arguments that the __init__ method expects (excluding self, which Python handles for you).

# creating_objects.py

class Dog:
def __init__(self, name: str, age: int):
self.name = name
self.age = age
print(f"A new dog named '{self.name}' has been created!")

# Instantiate our first Dog object
dog_one = Dog("Buddy", 4)

# Instantiate a second, separate Dog object
dog_two = Dog("Lucy", 2)

# We can access the unique attributes of each object using dot notation
print(f"{dog_one.name} is {dog_one.age} years old.")
print(f"{dog_two.name} is {dog_two.age} years old.")

Output:

A new dog named 'Buddy' has been created!
A new dog named 'Lucy' has been created!
Buddy is 4 years old.
Lucy is 2 years old.

As you can see, dog_one and dog_two are two distinct objects, both created from the Dog class, but each with its own separate data.


✨ Conclusion & Key Takeaways

You've just learned the foundational concepts of Object-Oriented Programming. This idea of creating blueprints (classes) to produce unique but consistently structured instances (objects) is one of the most powerful organizational tools in modern programming.

Let's summarize the key takeaways:

  • OOP bundles data and behavior.
  • A Class is a blueprint or template for creating objects.
  • An Object is a specific instance of a class, with its own data.
  • The class keyword is used to define a class.
  • The __init__() method is the constructor, used to set up the initial state (attributes) of an object.
  • self refers to the specific instance of the object being created.

Challenge Yourself: Create a Car class. The __init__ method should take make, model, and year as arguments and set them as attributes on the car object. Instantiate two different Car objects from your class and print out their attributes.


➡️ Next Steps

Our objects have data (attributes), but they don't have any behavior yet. In the next article, we'll learn how to define "Defining Classes: Attributes and Methods" to make our objects do things.

Happy coding!