liveBook · Manning

Self parameter

As explained in The Quick Python Book, Fourth Edition

self is the conventional first parameter name that refers to the instance on which a Python method is invoked.

In a Python instance method, the first parameter refers to the instance on which the method is invoked. Its conventional name is `self`. You write it in the method definition, but do not pass it explicitly in an ordinary call such as `circle.area()`; Python supplies `circle` as `self`. section 15.3

An instance field and a local parameter

`class Circle: def __init__(self, radius=1): self.radius = radius def area(self): return self.radius * self.radius * 3.14159` `c = Circle(3)` passes the new instance to `__init__` as `self`. Inside that method, `radius` is a local parameter; `self.radius` is a field on the instance. Calling `c.area()` returns `28.27431`. `__init__` initializes the new instance; it does not construct it. section 15.3 section 15.1.1

A method can also be called through its class: `Circle.area(c)` supplies the instance explicitly and gives the same result as `c.area()`. This form is less convenient and rarely needed. Through `self`, a method can access instance fields, class members, and accessible inherited members; ordinary local variables are not instance fields. section 15.3 section 15.11

The rule differs for other kinds of methods: a static method receives no `self`, while a class method receives a reference to the class, conventionally named `cls`. section 15.15

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains implicit argument passing, explicit class-based calls, and namespace access without descending into method implementation internals.
Examples
Uses a circle’s radius and area to distinguish `self.radius` from a local parameter and compare method-call forms.
What it assumes
Assumes familiarity with object-oriented programming while explaining Python’s explicit first parameter and instance-field syntax.
What you can do after
Evaluate/understand it when reading method definitions and calls, including which instance supplies a method’s first argument.
How it connects
Connects instance methods to `__init__`, instance variables, inheritance, static methods, and class methods.

Also explained in

Compare how 3 books explain self parameter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.