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
Class Attribute
class Dog:
legs = 4
print(Dog.legs)
print(Dog().legs)Instance Attribute
class Dog:
def __init__(self, name):
self.name = name
d1 = Dog('Rex')
d2 = Dog('Buddy')
print(d1.name, d2.name)Attribute Lookup Order
class C:
x = 10
c = C()
c.x = 99 # instance shadows class attr
print(c.x) # 99
print(C.x) # 10Mutable Class Attribute Trap
class C:
items = []
C.items.append(1)
print(C().items) # [1] — shared!Solving the Mutable Trap
class C:
def __init__(self):
self.items = []
c1 = C(); c2 = C()
c1.items.append(1)
print(c1.items, c2.items)__dict__ Inspection
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
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
class MathUtils:
@staticmethod
def add(a, b): return a + b
print(MathUtils.add(3, 4))Tracking Instances with Class Attr
class Widget:
count = 0
def __init__(self):
Widget.count += 1
Widget(); Widget(); Widget()
print(Widget.count)hasattr() and vars()
class P:
def __init__(self, x): self.x = x
p = P(5)
print(hasattr(p, 'x'), hasattr(p, 'z'))
print(vars(p))Quick Check
Recap
Keep Going
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
- Classes and the __init__ Method
- Instance vs Class Attributes
- Instance Methods and self
- Magic Methods: __str__ and __repr__