Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to define a class in Python

Understanding Classes in Python

Imagine a blueprint for a house. It details the number of rooms, the design of the kitchen, the size of the yard, and so on. You can think of a class in Python as a similar kind of blueprint for creating objects (specific instances of the class). Let's learn how to define a class in Python.

Basics of Defining a Class

Defining a class in Python is straightforward. Here's the most basic form:

class MyFirstClass:
    pass

In this example, class is a keyword that indicates you're defining a class. MyFirstClass is the name of the class, and pass is a keyword that means "do nothing". This class doesn't do much, but it's a valid class definition in Python.

Adding Attributes to Classes

Now, let's make our classes more useful. A class can have attributes (think of these as variables that belong to the class) that define its properties. Let's add an attribute to our class:

class House:
    color = 'white'

In this example, color is an attribute of the House class, and its value is 'white'. We can access this attribute using the dot (.) operator:

>>> House.color
'white'

Creating Instances (Objects) from Classes

A class by itself is like a blueprint, and it doesn't do much good until you create an instance (or object) of the class. You can think of an instance as a house built from the blueprint.

class House:
    color = 'white'

my_house = House()

my_house is now an instance of the House class. We can access its color attribute just like we did with the class:

>>> my_house.color
'white'

Defining Methods in a Class

Methods are like functions that belong to a class. They define the behaviors that an instance of the class can perform. Let's add a method to our House class:

class House:
    color = 'white'

    def paint(self, new_color):
        self.color = new_color

In this example, paint is a method that changes the color of the house. The self parameter is a reference to the current instance of the class, and it's how we access the instance's attributes and other methods.

Let's create an instance of House and call its paint method:

>>> my_house = House()
>>> my_house.color
'white'
>>> my_house.paint('blue')
>>> my_house.color
'blue'

As you can see, calling the paint method on my_house changes its color attribute.

The init Method

The __init__ method is a special method that gets called when you create a new instance of a class. It's often used to set up attributes with initial values:

class House:
    def __init__(self, color):
        self.color = color

    def paint(self, new_color):
        self.color = new_color

Now, when we create a new House, we need to provide a color:

>>> my_house = House('red')
>>> my_house.color
'red'

Conclusion

Classes are like blueprints, and instances are the real-world objects created from those blueprints. Each class can have attributes (like the color of a house) and methods (like painting a house). The __init__ method sets up new instances with initial values.

It's like being an architect and a builder in one. You design your blueprint (class) with all its properties (attributes) and actions (methods), then you bring it to life by building houses (instances). You've gained the power to create and control your own Python world. So, put on your hard hat, roll up your sleeves, and start building!