Instance Attributes
& Methods
Learn how to define instance attributes and methods in Python classes, and master the role of self in connecting data to behavior.
What Is an Object?
In Python, an object is a bundle of data (attributes) and behavior (methods) stored together. Think of a class as a blueprint and each instance as a unique building constructed from that blueprint.
What Are Instance Attributes?
An instance attribute is a variable that belongs to one specific object. It is created inside the __init__ method using the self keyword, and each object stores its own value.
What Are Methods?
A method is a function defined inside a class that operates on an instance's data. The first parameter is always self, which gives the method access to that instance's attributes.
The Role of self
The __init__ Constructor
The __init__ method is Python's constructor. It runs automatically every time you create a new instance. Its job is to initialize instance attributes.
Instance vs. Class Attributes
| Instance Attribute | Class Attribute | |
|---|---|---|
| Defined in | __init__ via self | Class body (outside methods) |
| Scope | Unique per object | Shared by all instances |
| Access | self.attr or obj.attr | ClassName.attr or obj.attr |
| Example | self.name = "Rex" | species = "Canis lupus" |
| Typical use | Data that varies per object | Constants or counters |
Step-by-Step: Building a Class
Example 1: Student Class
Example 2: BankAccount
Example 3: Rectangle with Math
How self Flows at Runtime
Methods with Extra Parameters
Methods can accept additional arguments after self. The caller passes them normally, and self is still injected automatically by Python.
Methods Calling Other Methods
Common Mistakes
Summary Cheat Sheet
| Concept | Syntax | Purpose |
|---|---|---|
| Class definition | class MyClass: | Creates the blueprint |
| Constructor | def __init__(self, ...): | Initializes instance |
| Instance attribute | self.attr = value | Stores per-object data |
| Method | def method(self, ...): | Defines behavior |
| Create instance | obj = MyClass(args) | Builds a new object |
| Call method | obj.method(args) | Runs behavior on obj |
Question 1 of 5
What is the purpose of the self parameter in a Python method?
Question 2 of 5
Code:
class Cat:
def __init__(self, name):
name = name
whiskers = Cat("Whiskers")
print(whiskers.name)
What happens when this code runs?
Question 3 of 5
Code:
class Timer:
def __init__(self):
self.seconds = 0
def tick(self):
self.seconds += 1
t1 = Timer()
t2 = Timer()
t1.tick()
t1.tick()
t2.tick()
After this code runs, what are the values of t1.seconds and t2.seconds?
Question 4 of 5
Which line correctly defines an instance method that returns the object's name attribute?
Question 5 of 5
Code:
class Playlist:
def __init__(self, name):
self.name = name
self.songs = []
def add_song(self, song):
self.songs.append(song)
def count(self):
return len(self.songs)
rock = Playlist("Rock Hits")
rock.add_song("Bohemian Rhapsody")
rock.add_song("Stairway to Heaven")
jazz = Playlist("Jazz Vibes")
What does rock.count() return and what does jazz.count() return?