self

The first parameter of a method: the object the method was called on. It gives access to that object’s attributes and other methods.

On this page

Example

#
Python
class Student:
    def __init__(self, name):
        self.name = name
        self.points = 0

    def add_points(self, amount):
        self.points += amount
        return self.summary()

    def summary(self):
        return f"{self.name}: {self.points} pts"

emma = Student("Emma")
jack = Student("Jack")

print(emma.add_points(30))
print(jack.add_points(10))
print(Student.add_points(emma, 5))
Resultsaved result, you can check it
Emma: 30 pts
Jack: 10 pts
Emma: 35 pts

Definition and usage

#

Every ordinary method of a class receives the object it was called on as its first argument. By convention, this parameter is named self. Through it, the method reads and changes the data of that specific object: self.points += 10 adds points only to the student the method was called on.

Python turns the call emma.add_points(10) into Student.add_points(emma, 10). That is why a method definition has one more parameter than its call: the object before the dot lands in self automatically. For the same reason, inside a method you write that object’s other methods and attributes with the self. prefix, e.g. self.summary().

self is not a keyword, only a widely followed convention. The parameter could be named something else, but such code would confuse every reader. Unlike this in JavaScript or Java, self has to be explicitly written as the first parameter of every ordinary method.

An attribute assigned through self.name = ... belongs to a single object. A variable written directly in the class body is a class attribute, shared by all of its objects. Reading self.name checks the object first, then the class, but an assignment through self creates or changes an attribute of the object itself, leaving the class attribute unchanged.

Syntax

#
Syntax
class Name:
    def method(self, arg):
        self.attribute = arg
        return self.other_method()

object.method(arg)

More examples

#
Class attributes and object attributes
Python
class Student:
    school = "CodeWorlds"
    count = 0

    def __init__(self, name):
        self.name = name
        Student.count += 1

emma = Student("Emma")
jack = Student("Jack")
jack.school = "School No. 5"

print(emma.school, jack.school, Student.school)
print(Student.count, vars(jack))
Resultsaved result, you can check it
CodeWorlds School No. 5 CodeWorlds
2 {'name': 'Jack', 'school': 'School No. 5'}
A missing self in the definition
Python
class Lesson:
    def start():
        return "Starting the lesson"

try:
    Lesson().start()
except TypeError as error:
    print(error)
Resultsaved result, you can check it
Lesson.start() takes 0 positional arguments but 1 was given

Python passed the object as the first argument, but the method had no parameter to receive it.

Good practices

#
  • Always name the first parameter of an ordinary method self. Editors, linters, and every reader of the code recognize this convention.
  • The message "takes 0 positional arguments but 1 was given" when calling a method usually means its definition is missing self.
  • Change shared counters and settings through the class name, e.g. Student.count += 1. An assignment through self creates a separate attribute in just one object.

Related entries

#

Spotted a mistake or missing an example? Write to us.