Understanding Methods and Self
Introduction: Functions That Belong to a Class
Welcome to Understanding "self" in Python! This is the very first unit of the course, and it starts exactly where the previous course left off. By now, we can define a class, create independent objects from it, attach attributes with dot notation, and write plain functions such as describe(dog) that take an object and read its attributes.
That pattern works, but it has a weak spot: those functions live outside the class, often far away in the file, and nothing formally connects them to the objects they were written for. A reader has to guess that describe(dog) was meant for Dog objects.
In this lesson, we fix that by moving the function inside the class body, turning it into a method, and introducing the parameter named self. By the end, we will be able to write a method, call it with object.method(), and explain precisely which object self stood for.
The Starting Point We Already Know
Let us begin with code that should feel completely familiar: a minimal Dog class, one object with attributes attached from the outside, and a top-level function that formats a description.
There is one detail worth noticing before we change anything: the entire job of describe is to work with one dog, and that dog is its first (and only) parameter. The function is already object-shaped; it simply lives in the wrong place.
Moving the Function Inside the Class
The change is smaller than it sounds. We take the same function, indent it into the class Dog: body, and rename its parameter to self.
What actually changed, and what did not:
- Indentation: the
defis now nested inside the class, so it belongs toDog. - Parameter name:
dogbecameself, a naming convention that Python programmers follow almost universally. - The body: unchanged in spirit; it still reads three attributes from one object, just through
selfinstead ofdog.
A function defined inside a class is called a method. And self is not a Python keyword: we could name it anything, but virtually all idiomatic Python code uses self, and Python’s style conventions strongly recommend it.

