Objects Interacting Through Methods
Introduction: Two Objects, One Method Call
Welcome to Objects Interacting with Other Objects in Python! This is the first unit of the course, and it marks an important shift in how we think about our code. So far, every method we wrote was focused on a single object: self read its own attributes, changed its own state, and called its own other methods.
Real programs rarely stay that lonely. A player attacks a monster, a bank account transfers money to another account, and a chat user sends a message to another user. In each case, two objects of the same kind are involved, and one of them acts on the other.
In this lesson, we will build a small videogame scenario with two warriors, in which one attacks the other. The key insight is simple but powerful: a method can accept another instance of its own class as a parameter, and we reach both objects with the exact same dot notation we already know.
Setting the Stage: A Class With State Worth Changing
Before two objects can interact, each of them needs some state worth changing. Let's start with a Warrior class whose constructor stores three attributes, using the __init__ pattern from the previous course.
Now we create two warriors with different values:
As we may recall, each instance keeps its own independent copy of these attributes: changing archer.health leaves knight.health completely alone. This class is a great fit for interaction because the attributes naturally point at each other: one warrior's power is exactly the number that should reduce another warrior's health.
A Method That Reads Another Object
Let's begin gently, with a method that only reads from a second warrior without changing anything. We simply add a second parameter after self:
Two objects are now in play inside the same method body:
selfis filled in automatically by whatever object sits before the dot in the call;otheris the argument we pass inside the parentheses;otheris just a conventional parameter name, not a keyword; we could name ittargetorenemy, and Python would not care.
Notice that self.name and other.name look identical in structure. Once an object is stored in a variable, we access its attributes the same way, whether that variable is called self, other, or knight.

