liveBook · Manning

Self

As explained in Practices of the Python Pro

self is the conventional name for the Python method parameter that refers to the current object instance.

self is the conventional name for the parameter that refers to the current object instance. Methods receive it so they can access or change that instance’s state. section 2.2.2

Think of a class as a template and an object as one created copy. self lets each method work with that particular copy rather than with unrelated data. section 2.2.2

Using self in a simulator

class RockPaperScissorsSimulator: def __init__(self): self.computer_choice = None self.human_choice = None def print_choices(self): print(self.human_choice) print(self.computer_choice) Each method receives self automatically when called through an instance. Assigning self.human_choice stores the value on that particular simulator object.

How Practices of the Python Pro covers it

How deep it goes
Explains self through class methods, initialization, attribute access, and a small game simulator.
Examples
Demonstrates self with a RockPaperScissorsSimulator that stores and prints human and computer choices.
What it assumes
Builds from classes, objects, methods, attributes, and initial state while introducing self as the instance reference.
What you can do after
Apply it via Python to initialize, update, and read data belonging to a particular object instance.
How it connects
Connects self to classes, objects, methods, attributes, instance state, and __init__.

Also explained in

Compare how 2 books explain self →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.