No history yet

Introduction to Python Classes

Blueprints for Your Code

So far, you've worked with data like numbers, strings, and lists. These are powerful, but sometimes you need to represent more complex things, like a user, a car, or a bank account. This is where classes come in.

A class is like a blueprint. It doesn't represent a specific car, but it defines what a car is. It has a color, a model, and a speed. It can also do things, like accelerate or brake.

class

noun

A blueprint for creating objects. A class defines a set of attributes and methods that the created objects will all have.

An object, on the other hand, is a specific car built from that blueprint. It might be a red Tesla that's currently stopped. Another object could be a blue Ford going 50 miles per hour. They are both cars, built from the same Car blueprint, but they are distinct, individual things.

In programming, we call these specific things instances of a class.

This concept of bundling data (attributes) and functionality (methods) together is a cornerstone of object-oriented programming (OOP).

Defining a Class

Let's create a simple Dog class. We use the class keyword, followed by the name of the class (by convention, CamelCase is used for class names).

class Dog:
    # This is a special method called the constructor.
    # It runs when a new Dog object is created.
    def __init__(self, name, breed):
        # These are attributes. They are variables that belong to an instance.
        self.name = name
        self.breed = breed

    # This is a method. It's a function that belongs to an instance.
    def bark(self):
        return f"{self.name} says woof!"

Let's break that down.

The __init__ method is special. It's called a constructor, and it gets run automatically whenever you create a new instance of the class. Its job is to set up the object's initial state.

The self parameter is also special. It refers to the instance of the class itself. When you create a dog named Fido, self inside the methods refers to Fido. When you create another dog named Lucy, self refers to Lucy. Python passes this self argument automatically, so you don't need to provide it when you call the method.

self.name = name creates an attribute called name and assigns it the value passed into the __init__ method. The same happens for breed.

Finally, bark is a method. It's a function defined inside a class that can be called on an instance of that class. Notice how it also takes self as its first parameter, giving it access to the instance's attributes like self.name.

Creating and Using Instances

Now that we have our Dog blueprint, let's create some actual dogs. This is called instantiation.

# Creating two instances of the Dog class
my_dog = Dog("Fido", "Golden Retriever")
another_dog = Dog("Lucy", "Poodle")

Here, my_dog and another_dog are two separate objects, or instances, of the Dog class. Each one has its own name and breed attributes. You can access these attributes using dot notation.

print(my_dog.name)        # Output: Fido
print(another_dog.breed)  # Output: Poodle

You can also call methods on these instances.

print(my_dog.bark())        # Output: Fido says woof!
print(another_dog.bark())   # Output: Lucy says woof!

When you call my_dog.bark(), Python is essentially doing Dog.bark(my_dog). This is why the self parameter is so important; it's how the method knows which object it's working with.

Classes allow you to model real-world (or abstract) concepts in a clean, reusable way. You define the blueprint once and can then create as many distinct objects from it as you need, each with its own state but sharing the same behavior.

Time to check your understanding.

Quiz Questions 1/5

In object-oriented programming, what is the relationship between a class and an object?

Quiz Questions 2/5

What is the special name of the method that is automatically called when you create a new instance of a class?

Now you have the building blocks for object-oriented programming in Python. Next, we'll see how you can build upon existing classes to create more specialized ones.