Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
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.
__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!
__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.
__init__
to accept argumentsWhen 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.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll explain concepts that new Python programmers often overlook.
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.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll share ideas new Pythonistas often overlook.