Classes are a way to bundle functionality and state together.
The terms "type" and "class" are interchangeable:
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.
When you define a class in Python, you'll see
self and why is it everywhere?
Point has three methods: an initializer, an
is_origin method, and a
class Point: def __init__(self, x, y, z): self.move_to(x, y, z) def is_origin(self): return self.x == self.y == self.z == 0 def move_to(self, x, y, z): self.x, self.y, self.z = x, y, z
We can make an instance of this class, by calling it. We can access attributes and can call methods on this class instance:
>>> p = Point(1, 2, 3) >>> p.x 1 >>> p.is_origin() False >>>
Each of the methods in our
Point class accepts
self as its first argument.
What do you think will happen, if we delete that
class Point: def __init__(x, y, z): self.move_to(x, y, z) def is_origin(): return self.x == self.y == self.z == 0 def move_to(x, y, z): self.x, self.y, self.z = x, y, z
Now when we call the
Point class to make a new
Point object, we'll see an error:
>>> p = Point(1, 2, 3) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __init__() takes 3 positional arguments but 4 were given >>>
The error says
__init__ (our initializer method) takes three positional arguments, but four were given.
We only passed three arguments into our
Point class; it got four arguments because
self was passed in as the first argument (before the three we specified).
So whether you like it or not, the first argument to every one of your methods is going to be
self, which means we need to capture this
self thing that's passed in as the first argument.
So what is
Let's temporarily change our
is_origin method here to return the
def is_origin(self): return id(self)
id function returns a number representing the memory location of a particular object.
If we call the
is_origin function, we get a number.
>>> p = Point(1, 2, 3) >>> p.is_origin() 139775673938080
If we look at the
id of the
p variable we made, we're going to get the same number:
>>> id(p) 139775673938080
p points to a
Point object (remember variables are pointers in Python).
self variable in our method call points to the same exact object.
self is really just a variable that points to the instance of our class that we're currently working with.
What if we take
self everywhere in the code and change it to
class Point: def __init__(this, x, y, z): this.move_to(x, y, z) def is_origin(this): return this.x == this.y == this.z == 0 def move_to(this, x, y, z): this.x, this.y, this.z = x, y, z
Would our code still work?
If we make a new
Point object again, we'll see that we everything works as it did before: we can still access attributes and we can still call methods:
>>> p = Point(1, 2, 3) >>> p.x 1 >>> p.is_origin() False
From Python's perspective, it doesn't actually matter, what you call
You just have to accept that the instance of your class will be passed in as the first argument.
self variable is just a very strong convention, you should call it
self (otherwise other Python programmers will be confused when reading your code) but you're allowed to call it something else.
When Python calls a method in your class, it will pass in the actual instance of that class that you're working with as the first argument.
Some programming languages use the word
this to represent that instance, but in Python we use the word
When you define a class in Python, every method that you define, must accept that instance as its first argument (called
self by convention).
self variable points to the instance of the class that you're working with.
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.