Search⌘ K
AI Features

Class Variable vs Instance Variable

Explore the distinctions between class and instance variables in Python's object-oriented programming. Understand how class variables are shared across instances while instance variables are unique to each object. Discover potential pitfalls when modifying these variables and how Python manages attribute access and shadowing.

In this section, we’ll demystify the difference between class and instance variables.

Difference between class variable and instance variable

There are two types of data attributes on Python objects:

  • Class variable
  • Instance variable

Class variable

  • A variable declared inside a class definition and not tied to any instance is called a class variable.
  • It stores content
...

Instance variable

  • A variable tied to a particular object instance is called an instance variable.
  • It stores content on each object
...