Stop 07 · Object-Oriented ProgrammingTopics · 3/10

Instance vs Class Attributes

Part of the Python Roadmap

In short

Some data belongs to each object, like a student's name. Some belongs to the whole group, like the school's name. Mixing them up leads to strange, shared-data bugs.

Instance attributes (set on self) belong to one object, while class attributes are shared by every object of that class. Use class attributes for constants or counters that apply to all instances.

The jaadu pins one big notice on a school corridor board while several students each wear their own different name badge.

Think of it like this

Your name tag vs the school notice board

Each student wears their own name tag (instance attribute). The notice board in the corridor shows the school name and timings for everyone (class attribute). Change the notice board and every student sees it. Change one name tag and only that student is affected.

Watch it happen

Where does the attribute live?

Instance attribute

  • Set as self.x in __init__
  • Different for each object
  • Name, balance, marks
  • Changing it affects one object

Class attribute

  • Set in the class body
  • Shared by every object
  • School name, counters, constants
  • Changing it on the class affects all

Per-object data goes on self. Data that's truly the same for every object, like a tax rate or a count of objects created, goes on the class.

The big ideas

What you need to know

Lookup order

obj.x checks the object first, then the class. That's why every object can read a class attribute.

Assignment shadows

obj.school = 'X' creates a new instance attribute on that object only. The class value stays the same.

Counters

Update shared counters through the class name, like Student.count += 1, not self.count += 1.

Mutable class attributes

A list in the class body is shared by every object. Appending from one object changes it for all.

See it in code

Walk through the code

python
1class Student:2    school = "Kendriya Vidyalaya"   # class attribute3    count = 0                        # shared counter4 5    def __init__(self, name: str):6        self.name = name             # instance attribute7        Student.count += 18 9a = Student("Isha")10b = Student("Rahul")11 12print(a.school, b.school)   # same value for both13print(Student.count)        # 214 15Student.school = "DPS"16print(a.school)             # DPS, changed for everyone17 18a.school = "Private Tuition"19print(a.school)             # Private Tuition (only a)20print(b.school)             # DPS21print(a.__dict__)           # {'name': 'Isha', 'school': 'Private Tuition'}

1/5Values in the class body belong to the class and are shared.

A shared school name and counter on the class, and per-student names on each object.

Save yourself the pain

Common mistakes

Oops

Defining items = [] in the class body and appending per object.

Do this instead

Every object shares that one list. Create self.items = [] inside __init__ instead.

Oops

Writing self.count += 1 for a shared counter.

Do this instead

That creates an instance attribute on one object. Use Student.count += 1 or type(self).count += 1.

Your mission

Pizza shop orders

~20 min

Quick check

class Dog: tricks = []. If d1.tricks.append('sit'), what is d2.tricks?

Go deeper

  1. 1Add a class attribute bank_name shared by all accounts.
  2. 2Count how many objects have been created using a class attribute.
  3. 3Avoid mutable class attributes like lists, which are shared by every object.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge