Python Understanding self & Methods
self / attributes Topic 09 Practice

Understanding self and instance attributes

Learn how self refers to the current object, and how attributes are stored per instance.

Picture this: self.x belongs to this object

__init__ runs when you create the object. self.x = … sticks data on it.

Class()create
→
__init__setup
→
self.x = …this object’s x

What is self?

self is a reference to the current object. When you write self.name = "Max", you're storing a value in that specific object's storage, not shared across all objects.

Real-world analogy

Think of a class as a form template, and each instance as a filled-out form. self means "this form"—when you write on it, you're writing on THIS form, not every form.

UML Class (Person)

Person
- name: str
- age: int
+ __init__(name, age)
+ greet()

Object Snapshot Diagram

p1 → {name: "Alice", age: 25}
p2 → {name: "Bob", age: 30}

Each instance stores independent attribute values in memory.

Person class example

1class Person:
2def __init__(self, name, age):
3self.name = name
4self.age = age
5def greet(self):
6print(f"Hi, I'm {self.name}")

Instance: p1

Compare two different instances

Two independent instances

1p1 = Person("Alice", 25)
2p2 = Person("Bob", 30)
3p1.greet() # Hi, I'm Alice
4p2.greet() # Hi, I'm Bob