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.
Example
#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))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
#class Name:
def method(self, arg):
self.attribute = arg
return self.other_method()
object.method(arg)More examples
#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))class Lesson:
def start():
return "Starting the lesson"
try:
Lesson().start()
except TypeError as error:
print(error)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 throughselfcreates a separate attribute in just one object.
Related entries
#- classDefines a class: a template for objects that combines data with the functions that act on it.
- __init__()A method called automatically when an object is created. It stores the object’s initial data, its attributes.
- @classmethodA method that receives the class (cls) instead of an object. It is most often used as an alternative constructor.
- @staticmethodA method that gets neither the object nor the class automatically. It behaves like a plain function placed inside a class.
- @propertyTurns a method into an attribute: the value is computed on read, and an optional setter controls writes.
Spotted a mistake or missing an example? Write to us.