What is __init__ in Python?

Share
Copied to clipboard.
Series: Classes
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
2 min. read 3 min. video Python 3.8—3.12

Let's talk about the __init__ method in Python.

A pointless Point class

Here's a class called Point:

class Point:
    """2-dimensional point."""

We can construct a new instance of this class by calling it:

>>> p = Point()
>>> p
<point.Point object at 0x7fd239fecfd0>

We have a Point object at this point, but this Point object really has no point because it has no functionality (it doesn't store any useful data or have any methods).

We could manually add attributes to Point objects to store some data on them:

>>> p.x = 1
>>> p.y = 2
>>> p.x
1

But doing so would be a little silly.

It would be better if we could somehow call this class with arguments to store attributes automatically.

Python's initializer method: __init__

Currently, if we try to call this class with arguments, we'll see an error:

>>> p = Point(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Point() takes no arguments
>>>

In order to accept arguments, we need to define a __init__ method in our class.

    def __init__(self, x, y):
        self.x = x
        self.y = y

The first argument in our __init__ method will always be self (just like pretty much every other method). After that we need to declare any arguments we want our class to accept.

The main thing you'll pretty much always see in a __init__ method, is assigning to attributes.

This is our new Point class

class Point:
    """2-dimensional point."""
    def __init__(self, x, y):
        self.x = x
        self.y = y

If we call it like before without any arguments, we'll see an error because this class now requires two arguments, x and y:

>>> p = Point()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() missing 2 required positional arguments: 'x' and 'y'

We need to give our Point class two arguments in order to get a new instance of this class:

>>> p = Point(1, 2)

This Point object now has an x attribute and a y attribute:

>>> p.x
1
>>> p.y
2

That means our __init__ method was called!

Python calls __init__ whenever a class is called

Whenever you call a class, Python will construct a new instance of that class, and then call that class' __init__ method, passing in the newly constructed instance as the first argument (self).

Unlike many programming languages, __init__ isn't called the "constructor method".

Python's __init__ method is called the initializer method. The initializer method initializes our new class instance. So by the point that the initializer method is called the class instance has already been constructed.

Use __init__ to accept arguments

When you make a new class in Python the first method you'll likely make is the __init__ method. The __init__ method allows you to accept arguments to your class.

More importantly, the __init__ method allows you to assign initial values to various attributes on your class instances.

Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.