01 / 21

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.

Python

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.

class Dog Attributes name, breed, age Methods bark(), sit(), fetch() Blueprint Instance: rex name = "Rex" breed = "Labrador" age = 3 Instance: bella name = "Bella" breed = "Poodle" age = 5

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.

class Dog: def __init__(self, name, breed): self.name = name # instance attribute self.breed = breed # instance attribute

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.

class Dog: def __init__(self, name, breed): self.name = name self.breed = breed def bark(self): return f"{self.name} says Woof!"

The Role of self

self self.name stores data self.breed stores data self.bark() reads self.name self.sit() modifies state ATTRIBUTES METHODS

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.

When it runs
Immediately when you call the class name, e.g. Dog("Rex", "Lab")
What it does
Assigns the passed arguments to self.attribute so the object remembers them
Required param
First parameter must be self: Python passes the new object in automatically

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

1
Define the class with the class keyword and a descriptive name (PascalCase).
2
Write __init__(self, ...) listing every piece of data the object needs at creation.
3
Assign self.attr = value inside __init__ for each parameter to store it on the instance.
4
Add methods that take self as the first parameter and use self.attr to access or modify data.
5
Create instances by calling ClassName(args) and call methods with obj.method().

Example 1: Student Class

class Student: def __init__(self, name, grade): self.name = name self.grade = grade def is_passing(self): return self.grade >= 60 # Create instances alice = Student("Alice", 92) bob = Student("Bob", 55) print(alice.is_passing()) # True print(bob.is_passing()) # False

Example 2: BankAccount

class BankAccount: def __init__(self, owner, balance=0): self.owner = owner self.balance = balance def deposit(self, amount): self.balance += amount return self.balance def withdraw(self, amount): if amount > self.balance: return "Insufficient funds" self.balance -= amount return self.balance

Example 3: Rectangle with Math

class Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height def perimeter(self): return 2 * (self.width + self.height) def scale(self, factor): self.width *= factor self.height *= factor

How self Flows at Runtime

You write: rex.bark() Python translates to: Dog.bark(rex) So now: self = rex Inside bark(self): self.name "Rex" return "Rex says Woof!"

Methods with Extra Parameters

Methods can accept additional arguments after self. The caller passes them normally, and self is still injected automatically by Python.

class Counter: def __init__(self): self.count = 0 def increment(self, step=1): self.count += step def reset(self): self.count = 0 c = Counter() c.increment(5) # count is now 5 c.increment() # count is now 6 c.reset() # count is now 0

Methods Calling Other Methods

class Circle: def __init__(self, radius): self.radius = radius def area(self): import math return math.pi * self.radius ** 2 def describe(self): a = self.area() # calling another method return f"Circle r={self.radius}, area={a:.2f}"

Common Mistakes

✕ Forgetting self
Writing def bark(): instead of def bark(self): causes a TypeError when the method is called on an instance.
✕ Missing self. prefix
Writing name = name instead of self.name = name in __init__ creates a local variable that vanishes immediately.
✕ Passing self manually
Calling rex.bark(rex) passes rex twice. Python handles self automatically: just write rex.bark().

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

What happens when this code runs?

Question 3 of 5

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

What does rock.count() return and what does jazz.count() return?