0Pricing
Python Academy · Lesson

Instance vs Class Attributes

Differentiate between per-object and shared class-level data.

Instance vs Class Attributes is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Understanding the difference between instance and class attributes prevents subtle bugs and enables efficient shared state.

Class Attribute

class Dog: legs = 4 — legs is shared by all instances. Access as Dog.legs or dog.legs.
class Dog:
    legs = 4
print(Dog.legs)
print(Dog().legs)

Instance Attribute

self.name = name in __init__ is per-instance. Each Dog has its own name.
class Dog:
    def __init__(self, name):
        self.name = name
d1 = Dog('Rex')
d2 = Dog('Buddy')
print(d1.name, d2.name)

Attribute Lookup Order

Python first checks the instance __dict__, then the class __dict__, then base classes. Instance attributes shadow class attributes.
class C:
    x = 10
c = C()
c.x = 99  # instance shadows class attr
print(c.x)  # 99
print(C.x)  # 10

Mutable Class Attribute Trap

class C: items = [] — ALL instances share the same list. Appending on one instance affects all!
class C:
    items = []
C.items.append(1)
print(C().items)  # [1] — shared!

Solving the Mutable Trap

Move initialization to __init__: def __init__(self): self.items = [] Each instance gets its own list.
class C:
    def __init__(self):
        self.items = []
c1 = C(); c2 = C()
c1.items.append(1)
print(c1.items, c2.items)

__dict__ Inspection

obj.__dict__ is the instance attribute dictionary. Class.__dict__ is the class attribute dictionary.
class Dog:
    legs = 4
    def __init__(self, name): self.name = name
d = Dog('Rex')
print(d.__dict__)
print(Dog.__dict__.keys())

Class Methods as Factories

@classmethod receives cls (the class) instead of self. Use as alternative constructors.
class Date:
    def __init__(self, y, m, d): self.y,self.m,self.d=y,m,d
    @classmethod
    def from_string(cls, s):
        y,m,d=map(int,s.split('-'))
        return cls(y,m,d)
dt=Date.from_string('2024-01-15')
print(dt.y,dt.m,dt.d)

Static Methods

@staticmethod has no self or cls. Use for utility functions logically grouped with a class but not needing instance/class.
class MathUtils:
    @staticmethod
    def add(a, b): return a + b
print(MathUtils.add(3, 4))

Tracking Instances with Class Attr

A class attribute as a counter tracks how many instances have been created.
class Widget:
    count = 0
    def __init__(self):
        Widget.count += 1
Widget(); Widget(); Widget()
print(Widget.count)

hasattr() and vars()

hasattr(obj, 'name') checks attribute existence. vars(obj) is equivalent to obj.__dict__.
class P:
    def __init__(self, x): self.x = x
p = P(5)
print(hasattr(p, 'x'), hasattr(p, 'z'))
print(vars(p))

Quick Check

What happens when you append to a mutable class attribute list from an instance?

Recap

Class attributes are shared; instance attributes are per-object. Instance lookup shadows class. Initialize mutables in __init__. @classmethod for factory methods.

Keep Going

Excellent! Continue to the next lesson to deepen your skills.

Frequently asked questions

Is the “Instance vs Class Attributes” lesson free?

Yes — the full text of “Instance vs Class Attributes” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Instance vs Class Attributes”?

Differentiate between per-object and shared class-level data. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Instance vs Class Attributes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Classes and the __init__ Method
  2. Instance vs Class Attributes
  3. Instance Methods and self
  4. Magic Methods: __str__ and __repr__
← Back to Python Academy