Concepts / Understanding the self Parameter

Understanding the self Parameter

Constructors use the self parameter to initialize each new object and additional parameters to customize that object with specific data.

  • Programming

One Blueprint, Many Guests

Imagine tracking guests at a party. Every guest needs a name and a count of how many times that guest has danced. A single class can define the structure for every guest, but Sally's name and dance count must remain separate from Jim's. The self parameter is what lets the constructor attach each value to the particular object currently being initialized.

The class provides a blueprint. Each object created from that class receives its own attribute data.

Connecting Parameters to Attributes

Inside a constructor, self refers to the particular object being initialized. In self.name = nam, nam is the incoming parameter, while self.name is the name attribute belonging to that particular object. The assignment takes the value received through nam and stores it in the object's name attribute. This makes self the bridge between a generic class definition and a concrete object.

becomesowns attributeassigned by self.name = namnew objectthe object passed as selfselfthat particular objectnameSallynamincoming value
How does the value passed to the constructor become data stored on one specific object?

class PartyAnimal: def __init__(self, nam): self.x = 0 self.name = nam print(self.name, "constructed")

Creating Sally and Jim

Two constructor calls

Trace what happens when two PartyAnimal objects are created with different names.

Create s: When s = PartyAnimal('Sally') runs, Python creates a new object and passes that object as self to __init__. The string Sally is passed as nam.

Initialize s: The constructor sets s's x attribute to 0 and sets s's name attribute to Sally. It then displays Sally constructed and binds the object to s.

Create j: When j = PartyAnimal('Jim') runs, Python creates another object and runs the constructor again. This time self refers to the second object and nam receives Jim.

Initialize j: The second constructor call sets j's x attribute to 0 and j's name attribute to Jim. It then displays Jim constructed and binds the second object to j.

s and j refer to separate objects. Both begin with x equal to 0, but s has name Sally and j has name Jim.

runsinitializesthenrunsinitializesPartyAnimal('Sally')__init__ for snam = Sallysname = Sally, x = 0PartyAnimal('Jim')__init__ for jnam = Jimjname = Jim, x = 0
What happens step by step when two objects are created from the same class?

Why Counters Stay Independent

Each instance maintains its own copy of its attributes and occupies separate memory from other instances of the same class. Therefore, a method that increments s's x changes s's counter, not j's counter. If s.party() is called twice and j.party() is called once, s ends with x equal to 2 while j ends with x equal to 1. The shared class definition does not mean the objects share their attribute storage.

ownsownssname = Sallyx2x1jname = Jim
How can two objects from one class contain different values without changing each other?

What do you think happens?

Suppose s.party() is called, then j.party(), then s.party() again. What values should the two independent counters have?

  • s.x is 2 and j.x is 1
  • s.x is 1 and j.x is 1
  • s.x is 2 and j.x is 2
Reveal answer

Answer: s.x is 2 and j.x is 1

The first and third calls operate on s, so s's counter is incremented twice. The middle call operates on j, so j's separate counter is incremented once.

Output
For this sequence:

s.party()
j.party()
s.party()

s.x ends at 2, and j.x ends at 1. The name attributes remain Sally and Jim.

Reading State Changes

python

The important tracing habit is to identify self at every method call. During s.party(), self refers to the Sally object. During j.party(), self refers to the Jim object. When s.party() runs again, self refers to the Sally object again, so its existing x value is increased rather than reset. The constructor runs when each object is created; later method calls operate on the already initialized instance.

Mistakes with self

  • Treating nam and self.name as if they were the same thing.

    The parameter is the incoming value used during the constructor call. The self.name expression identifies the attribute belonging to the particular instance.

    Fix: Read self.name = nam as: store the value from nam in this object's name attribute.

  • Assuming that two objects created from one class share the same attribute values.

    Each instance has its own data storage and its own copies of its attributes.

    Fix: Track the object represented by self in each call. Changes made through s affect s's attributes, while changes made through j affect j's attributes.

  • Assuming the second constructor call reinitializes the first object.

    The second call creates a separate object and passes that different object as self.

    Fix: Keep a separate state record for every instance created from the class.

Practice the Trace

MEDIUM

A class creates two objects, a = PartyAnimal("Ava") and b = PartyAnimal("Ben"). The constructor gives every new object x equal to 0 and stores the supplied name. Then the program calls a.party(), a.party(), and b.party(). What are the final values of a.name, a.x, b.name, and b.x? Explain which object self refers to during each call.

Hints
  • Record the initial state immediately after each constructor call.
  • The two calls on a affect the same object.
  • The call on b affects b's independent x attribute.

Practice answer

Find the final state after a.party(), a.party(), and b.party().

Initial state: a has name Ava and x equal to 0. b has name Ben and x equal to 0.

First call: a.party() uses self for a, changing a.x from 0 to 1.

Second call: The next a.party() uses self for a again, changing a.x from 1 to 2.

Third call: b.party() uses self for b, changing b.x from 0 to 1. It does not change a.x.

a.name is Ava, a.x is 2, b.name is Ben, and b.x is 1.

Key Takeaways

  1. self identifies the particular object being initialized or manipulated.
  2. A constructor parameter such as nam carries incoming data, while an assignment such as self.name = nam stores that data on the current object.
  3. Every new PartyAnimal begins with its own x value of 0 and receives its own name value.
  4. Instances created from the same class occupy separate memory and maintain independent attribute values.
  5. To predict a program's state, follow which object self refers to during each constructor or method call.

Key Takeaways

  • self connects a constructor or method call to one particular object.
  • Assignments such as self.name = nam store incoming parameter values as attributes on that object.
  • Each instance has independent copies of its attributes, even when all instances come from the same class.
  • Tracking self at every call makes it possible to predict each object's final state and the program's output.