Let's talk about the __init__
method in Python.
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.
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!
__init__
whenever a class is calledWhenever 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.
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.
Classes are a way to bundle functionality and state together.
The terms "type" and "class" are interchangeable: list
, dict
, tuple
, int
, str
, set
, and bool
are all classes.
You'll certainly use quite a few classes in Python (remember types are classes) but you may not need to create your own often.
To track your progress on this Python Morsels topic trail, sign in or sign up.
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.
By signing up, you agree to the Privacy Policy.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll share ideas new Pythonistas often overlook.