- Courses
- 💻Programming Languages
- 🐍Python
- Object‑Oriented Programming (OOP)
- Classes & Objects
On this page
Classes & Objects
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:
- Classes — blueprints that describe how objects are structured
- Objects — concrete instances created from those blueprints
This lesson introduces these concepts and shows how to define and use them effectively.
What a Class Represents
A class defines:
- The attributes (data) an object will have
- The methods (functions) that operate on that data
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:
selfrefers to the specific object being created- Attributes like
self.nameandself.agebelong to that object - Each object receives its own independent values
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:
- A class is a blueprint for a house
- An object is an actual house built from that blueprint
The blueprint describes the structure; the house contains the actual materials.
Why Classes and Objects Matter
Classes and objects allow you to:
- Model real‑world entities naturally
- Group related data and behavior together
- Build scalable, maintainable software
- Reduce duplication by encapsulating logic inside objects
- Structure programs around meaningful abstractions
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!
Previous
Object‑Oriented Programming (OOP)
Next
Attributes & Methods
- Understanding Object‑Oriented Programming
- What a Class Represents
- Creating Objects (Instances)
- Initializing Objects with
__init__ - Instance Attributes
- Defining Methods
- The Relationship Between Classes and Objects
- Why Classes and Objects Matter
- Summary
