Module 4 Lesson 2: Classes and Objects
From theory to code. Learn how to define a Class in Python, create Objects from it, and use dot notation to access data.
Module 4 Lesson 2: Classes and Objects
In the last lesson, we learned that a Class is a blueprint and an Object is the real thing built from that blueprint. In this lesson, we’ll learn the Python syntax to bring these concepts to life.
Lesson Overview
In this lesson, we will cover:
- The
classKeyword: How to define your blueprint. - Instantiation: Creating an object from your class.
- Dot Notation: How to access attributes and methods.
- Multiple Objects: Creating many instances from one class.
1. Defining a Class
To define a class, we use the class keyword. By convention, class names in Python use PascalCase (every word starts with a capital letter, like MyCoolClass).
class Dog:
# For now, let's keep it empty
pass
2. Creating an Object (Instantiation)
To create an object from your class, you "call" the class as if it were a function.
# Create two distinct objects from the Dog class
my_dog = Dog()
your_dog = Dog()
print(my_dog) # This will print a cryptic memory address
3. Adding Attributes (Dot Notation)
Even if a class is "empty," you can add data to its objects using a period (dot notation).
my_dog.name = "Buddy"
my_dog.breed = "Golden Retriever"
your_dog.name = "Max"
your_dog.breed = "Husky"
print(f"{my_dog.name} is a {my_dog.breed}.")
print(f"{your_dog.name} is a {your_dog.breed}.")
Think of it like this: my_dog is a container. my_dog.name is a specific label inside that container.
4. Why This is Better Than Dictionaries
You might be thinking: "Wait, couldn't I just use a dictionary for this?"
dog_dict = {"name": "Buddy", "breed": "Golden Retriever"}
Yes, you could! But classes offer two major advantages:
- Structure: A class guarantees that every "Dog" follows the same rules and has the same features.
- Behavior: Dictionaries only store data. Classes can store both data AND the functions (methods) that work on that data.
Practice Exercise: The Vehicle Class
Create a file named vehicles.py.
- Define a class named
Car. - Create two objects from that class:
car_1andcar_2. - Assign
make,model, andyearattributes to both cars using dot notation. - Print the details of both cars in a clear format.
Quick Knowledge Check
- How do you start a class definition in Python?
- What is the naming convention for classes (e.g., lowercase or PascalCase)?
- What is the technical term for "creating an object from a class"?
- How do you access an attribute of an object in code?
Key Takeaways
classdefines a new type of object.- Objects are "instances" of a class.
- Dot notation (
object.attribute) is used to get or set data. - Each object has its own data, even if they share the same class blueprint.
What’s Next?
Assigning attributes one-by-one (dog.name = "...") is slow and error-prone. In Lesson 3, we’ll learn about the most important method in OOP: The __init__ Method, which lets us set attributes automatically the moment an object is born!