Chapter 14: Thinking in Objects - An Introduction to Object-Oriented Programming
Up to this point, you've been writing code in a procedural way. You write a sequence of steps (statements, loops, function calls) for the computer to follow. This is a great way to start, but as your programs grow larger, it can become difficult to manage all the different variables and functions.
Object-Oriented Programming (OOP) is a different way of thinking. Instead of focusing on a sequence of actions, you start to think about your program in terms of "objects" that have their own data and behaviors.
This might sound abstract, but you already do this in the real world. Think about a dog. A dog isn't just a collection of functions like bark(), eat(), and sleep(). A dog is an object that has data (attributes) like its name, age, and breed, and it has behaviors (methods) like barking, eating, and sleeping.
OOP allows us to model real-world things like this directly in our code.
Classes and Objects: The Blueprint and the House
There are two central concepts in OOP:
The Class: A class is a blueprint for creating objects. It defines the attributes and methods that all objects of that type will have. For example, we could create a
Dogclass that says every dog must have anameand anage, and every dog must be able tobark().The Object (or Instance): An object is a specific instance created from a class. If
Dogis the blueprint, then your specific dog, Fido, who is 5 years old, is an object or instance of theDogclass. Another dog, Lucy, who is 2, would be a separate object. Both were built from the sameDogblueprint.
Creating Your First Class
Let's create a simple Dog class. The convention is to use PascalCase (capitalize the first letter of each word) for class names.
class Dog:
pass # The 'pass' keyword means do nothing. It's a placeholder.
This is a valid, though very boring, class. Let's make it more useful.
The Constructor: __init__()
__init__()How do we give a dog its specific name and age when we create it? We use a special method called the constructor. In Python, this method is always named __init__ (with two underscores on each side).
The __init__ method is called automatically every time you create a new object from the class.
What is self?
self?You'll notice the self parameter in __init__. self refers to the specific object instance being created. When we write self.name = name, we are saying, "Take the name that was passed in and assign it to this specific dog's name attribute." This is how the data gets attached to the object. self is always the first parameter of any method inside a class.
Accessing Attributes
Once you have an object, you can access its attributes using dot notation.
Adding Methods (Behaviors)
Now, let's give our Dog class some behaviors by defining methods. A method is just a function that is defined inside a class.
Notice that even though bark has self as a parameter, we don't pass it in when we call my_dog.bark(). Python does that for us automatically in the background.
Summary and What's Next
This has been a big conceptual leap! Let's review:
OOP is a way of modeling your program around objects.
A Class is the blueprint (e.g.,
Dog).An Object is a specific instance of a class (e.g., your dog, Rex).
Attributes are the data associated with an object (e.g.,
self.name).Methods are the behaviors of an object (e.g.,
bark()).The
__init__method is the constructor that runs when a new object is created.selfrefers to the specific instance of the object.
Object-Oriented Programming is a massive topic, and this is just the beginning. In the next chapter, we'll explore inheritance, a powerful OOP feature that allows us to create new classes based on existing ones.
Practice Time!
Create a
Carclass. Its__init__method should takemake,model, andyearas arguments.Add a method to the
Carclass calleddisplay_infothat prints out the car's make, model, and year.Create two different
Carobjects from your class and call thedisplay_infomethod on both.
Last updated