On this page

Classes & Objects

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Understanding Object‑Oriented Programming​

Object‑oriented programming (OOP) is a programming paradigm that models software as a collection of objects—each representing a real‑world or conceptual entity. Python’s OOP system allows you to structure programs around data and the behaviors that operate on that data.

Two fundamental concepts form the basis of OOP:

This lesson introduces these concepts and shows how to define and use them effectively.

What a Class Represents​

A class defines:

A class is a template. It does not hold data itself; it describes how objects created from it will behave.

Example of a simple class:


class Person:

    pass

This defines a class named Person. It does nothing yet, but it establishes the structure from which objects can be created.

Creating Objects (Instances)​

An object is an instance of a class. Creating an object is called instantiation.


p = Person()

Here, p is an object of type Person. Each object created from a class is independent and maintains its own data.

Initializing Objects with __init__​

To give objects initial data, classes define an initializer method named __init__. This method runs automatically when an object is created.


class Person:

    def __init__(self, name, age):

        self.name = name

        self.age = age

Creating an object:


p = Person("Alice", 30)

Key points:

Instance Attributes​

Instance attributes store data unique to each object.


print(p.name)

print(p.age)

Each object has its own attribute values:


p1 = Person("Alice", 30)

p2 = Person("Bob", 25)

p1 and p2 are independent objects with their own data.

Defining Methods​

Methods are functions defined inside a class. They operate on the object’s data.


class Person:

    def __init__(self, name):

        self.name = name



    def greet(self):

        return f"Hello, I am {self.name}."

Calling a method:


p = Person("Alice")

p.greet()

Methods always take self as their first parameter, which refers to the object calling the method.

The Relationship Between Classes and Objects​

A class defines what an object is and how it behaves.

An object is a specific instance of that class with its own data.

Analogy:

The blueprint describes the structure; the house contains the actual materials.

Why Classes and Objects Matter​

Classes and objects allow you to:

OOP becomes increasingly important as programs grow in complexity.

Summary​

In this lesson, you learned the foundational concepts of object‑oriented programming: classes, objects, attributes, methods, and initialization. You now understand how to define a class, create objects from it, and structure data and behavior together. These concepts form the basis for all advanced OOP topics that follow.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Object‑Oriented Programming (OOP)

Next

Attributes & Methods