Hello! Welcome to your first lesson on Object-Oriented Programming.
In the last module, you became proficient with Python's core built-in data structures like lists, tuples, and dictionaries. We ended by noting that while a dictionary is great for storing related data (like a user's name and score), it doesn't bundle the actions that user can perform.
Today, we take a major conceptual leap. We'll move beyond using pre-defined data types and explore how to define our own. This lesson introduces classes and objects, the fundamental concepts of Object-Oriented Programming (OOP). Your goal is to explain what classes and objects are and how they relate to each other. We will focus entirely on the idea behind them, preparing you to write the actual code in our next lesson.
1. From Data to Objects
So far, we've bundled data together using dictionaries. For example, to represent a dog, we might write:
dog1 = {
"name": "Fido",
"breed": "Golden Retriever",
"age": 5
}
This is good, but it has limitations:
- No associated actions: What if we want to define what a dog can do? For example, a
bark()action or anincrease_age()action. A dictionary doesn't naturally include functions. - No enforced structure: If we create another dog,
dog2, we could accidentally misspell a key ("naem") or forget one entirely. There's no blueprint to ensure all "dog" data structures are consistent.
To solve this, programming languages provide a way to create our own data types that bundle both data and the functions that operate on that data. This is the core idea behind Object-Oriented Programming.
2. The Class: A Blueprint
A class is a blueprint for creating objects. Think of it as a template or a design that specifies what properties and behaviors a certain type of thing will have.
For example, an architect's blueprint for a house is not a house itself. It's a detailed plan that defines the house's structure: it will have a certain number of rooms, doors, and windows.
In programming, a class defines:
- Attributes: The data or properties (e.g., for a
Dogclass, attributes would bename,breed, andage). - Methods: The functions or behaviors (e.g., for a
Dogclass, methods could bebark()andsit()).
To get a better feel for this, the following video explains why classes are so useful for logically grouping data and functions together.
Python OOP Tutorial 1: Classes and Instances
This video from Corey Schafer's popular Python OOP series explains the motivation behind using classes.
Watch the first minute of the video (00:29 to 01:38). Pay close attention to the distinction between 'attributes' (data) and 'methods' (functions associated with a class).
Analogies are a powerful way to understand the concept of a class. Let's look at a few more.
Mastering Object-Oriented Programming Through Simple Analogies
This short article provides several excellent analogies to help solidify the concept of a class as a blueprint.
Read the first section, '1. Class and Object ā The Building Blocks of OOP'. Focus on the three analogies: Blueprint and House, Student Template, and Car Factory. Notice how each one separates the design from the final product.
3. The Object: An Instance of a Class
If a class is the blueprint, then an object is the actual thing built from that blueprint. An object is often called an instance of a class.
- Blueprint: The
Houseplan (a class). - Objects/Instances: The specific houses built from the plan at 123 Maple St. and 456 Oak Ave.
Each object created from the same class shares the same structure (attributes and methods), but it has its own unique data. The house at 123 Maple St. might be painted blue, while the one at 456 Oak Ave. is painted green. They are both House objects, but they are separate and have different attribute values.
This image provides a perfect visual summary:

The process of creating an object from a class is called instantiation.
This next video offers a clear, formal definition of classes and objects, using the analogy of a pastry cutter creating many cookies (objects) from one mold (class).
Fundamental Concepts of Object Oriented Programming
Let's watch a concise explanation from Computer Science Lessons to formalize these definitions.
Watch from 01:31 to 03:28. Listen for the key terms: 'class' as a template, 'object' as an instance, and 'instantiation' as the act of creating an object.
4. A Glimpse of Classes and Objects in Python
While we will focus on the syntax in the next lesson, seeing it in action can make the concept more concrete. In Python, you create two distinct objects (instances) from a single class like this:
# The 'class' keyword defines the blueprint.
# We'll leave it empty for now with 'pass'.
class Employee:
pass
# We are now 'instantiating' two Employee objects.
# emp_1 and emp_2 are two separate instances of the Employee class.
emp_1 = Employee()
emp_2 = Employee()
print(emp_1)
print(emp_2)
If you were to run this code, the output would look something like this (the memory addresses will be different on your machine):
<__main__.Employee object at 0x000001A8A7A43FA0>
<__main__.Employee object at 0x000001A8A7A43F70>
The key thing to notice is that even though emp_1 and emp_2 were created from the same Employee class, they are separate, unique objects stored at different locations in memory.
The following video demonstrates this exact point.
Python OOP Tutorial 1: Classes and Instances
Let's watch Corey Schafer demonstrate the creation of two unique instances from a simple Employee class in Python.
Watch from 01:38 to 03:08. The main takeaway is seeing that employee_1 and employee_2 are two distinct objects, which he proves by showing they have different memory addresses.
5. Conceptual Check
Let's check your understanding of these new concepts.
- Imagine a class named
Book. What are three possible attributes (data) this class might have? - What is one possible method (action) the
Bookclass could have? - If
Bookis the class, is "The Hobbit" an object, an attribute, or a method? - True or False: If you create ten objects from one class, all ten objects must have the same attribute values.
Answers
1. Possible attributes for a `Book` class could be `title`, `author`, `page_count`, or `genre`. 2. A possible method could be `open_to_page(page_number)` or `get_summary()`. 3. "The Hobbit" would be a specific **object** (or instance) of the `Book` class. Its `title` attribute would have the value "The Hobbit". 4. False. All ten objects will share the same *structure* (the same set of attributes), but the *values* for those attributes can be different for each object.
Conclusion
In this lesson, you've learned the two most fundamental concepts in Object-Oriented Programming.
Key Takeaways:
- A class is a blueprint or template that groups related data (attributes) and functions (methods).
- An object is a unique instance created from a class. It has the structure defined by the class but holds its own specific data.
- The process of creating an object from a class is called instantiation.
- This approach allows us to model real-world entities in our code, leading to programs that are more organized, reusable, and easier to understand.
You've built a strong conceptual foundation. In the next lesson, we will put this theory into practice by learning how to define a simple class in Python using the class keyword and give it its first attributes.