0Pricing
Python Academy · Lesson

Instance vs Class Attributes

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

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)

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