Concepts / Instance Variables vs. Class Variables

Instance Variables vs. Class Variables

A list is an example of usage of objects and classes. When we use a variable i and assign a value to it, say integer 5 to it, you can think of it as creating an object (i.e. instance) i of class (i.e. type) int . In fact, you can read help(int) to understand this better.

  • Programming

What Are Instance and Class Variables?

When you create a class, you can define fields—variables that belong to that class. These fields come in two distinct flavors. Instance variables belong to each individual object created from the class; every instance gets its own copy. Class variables, by contrast, belong to the class itself and are shared across all instances of that class. Think of a class as a blueprint for a house: the instance variables are like the furniture inside each house (each house has its own set), while class variables are like the architectural style (all houses built from the same blueprint share that style).

Instance variables are accessed and modified on individual objects using dot notation (for example, myobject.field). Class variables are accessed through the class name or through instances, but changes to a class variable affect all instances simultaneously.

Understanding Scope and Lifetime

The fundamental difference between these two variable types lies in their scope and lifetime. An instance variable exists only for the lifetime of a specific object. When you create a new instance, that instance gets its own set of instance variables. When that instance is destroyed, its instance variables disappear. A class variable, however, exists for the lifetime of the class itself—from the moment the class is defined until the program ends. It persists even if no instances of the class currently exist.

definescreatescreatesownsownsownsownsClass DefinitionClass Variable: count0nameAlicenameBobInstance 1age25age30Instance 2
When you create multiple instances of a class, which variables are shared across all instances and which belong to each instance individually?

The diagram above shows how a class variable (count) lives at the class level and is shared, while instance variables (name and age) are stored separately for each instance. Each instance has its own copy of name and age, but all instances reference the same class variable.

How Storage and Access Work

Instance variables are stored in the memory space allocated for each individual object. When you access an instance variable through an object, the runtime looks up that specific object's memory location and retrieves the value stored there. Class variables are stored in the memory space allocated for the class itself, not in any individual instance. When you access a class variable through an instance, the runtime first checks if the instance has its own version of that variable; if not, it looks up the class-level variable.

references for class variablesreferences for class variablesClass Memory SpaceClass Variable: count= 0name = Alice, age =25name = Bob, age = 30Instance 1 MemorySpaceInstance 2 MemorySpace
Where in memory do instance variables live compared to class variables, and how does the runtime access them?

This two-tier lookup mechanism is crucial to understanding how class variables work. When you modify a class variable, you are changing the value in the class's memory space, so all instances see that change. When you modify an instance variable, you are changing only that instance's copy, leaving other instances unaffected.

Predicting Changes: What Happens When You Modify a Variable?

What do you think happens?

Imagine a class called Counter with a class variable count = 0 and instance variables name and value. If you create two instances and modify the class variable count to 5 through one instance, what value will the other instance see when it accesses count?

  • The other instance will still see 0, because each instance has its own copy
  • The other instance will see 5, because class variables are shared
  • The other instance will see an error
Reveal answer

Answer: The other instance will see 5, because class variables are shared

Class variables are stored at the class level, not at the instance level. When you modify a class variable through one instance, you are modifying the single shared copy that all instances reference. Therefore, all instances immediately see the new value.

referencesreferencesreferencesreferencescount = 0Instance 1 sees count= 0count = 5Instance 1 sees count= 5Instance 2 sees count= 0Instance 2 sees count= 5
If I change a class variable, what happens to all instances? If I change an instance variable, does it affect other instances?

Worked Example: A Student Class

Distinguishing Instance and Class Variables in Practice

You are designing a Student class. Each student should have their own name, ID, and grade (instance variables). The school also wants to track the total number of students created (class variable). Explain which variables should be instance variables and which should be class variables, and predict what happens when you create two students and modify the total count.

Identify what belongs to each student: Name, ID, and grade are specific to each individual student. If student Alice has a different name and grade than student Bob, these must be instance variables. Each instance needs its own copy.

Identify what belongs to the class: The total number of students is a property of the class itself, not of any individual student. All instances should share this count. This is a class variable.

Predict the effect of modifying the class variable: When you create the first student, the class variable total_students increments to 1. When you create the second student, it increments to 2. Both students, when they access total_students, see the same value: 2. There is only one copy of total_students in memory, shared by all instances.

Predict the effect of modifying an instance variable: If you change Alice's grade to 95, Bob's grade remains unchanged. Each instance has its own grade variable. Modifying one instance's variable does not affect other instances.

Instance variables (name, ID, grade) give each student their own data. The class variable (total_students) provides a shared counter that all instances can access and that reflects the total number of students ever created.

Common Mistakes

  • Assuming that modifying a class variable through one instance does not affect other instances

    If count is a class variable, there is only one copy shared by all instances. Modifying it through any instance changes the shared value for all instances.

    Fix: If you want each instance to have its own independent count, make count an instance variable initialized in the constructor, not a class variable.

  • Confusing class variables with instance variables when reading code

    Variables defined at the class level (outside methods) are class variables, not instance variables. Instance variables are typically defined inside the constructor or other methods.

    Fix: Look at where the variable is defined. If it is at the class level, it is a class variable. If it is defined inside a method (especially the constructor) using self, it is an instance variable.

  • Forgetting that class variables persist even when no instances exist

    Class variables belong to the class, not to instances. They exist for the lifetime of the class, independent of whether any instances currently exist.

    Fix: If you need a variable that resets for each instance, use an instance variable. If you need a persistent counter across all instances, use a class variable and manage its lifecycle explicitly.

  • Accidentally creating an instance variable that shadows a class variable

    When you assign to self.variable_name, you create an instance variable. If a class variable with the same name exists, the instance variable shadows it for that instance only.

    Fix: Be intentional about whether you want to modify the class variable or create an instance variable. Use ClassName.variable_name to access and modify class variables explicitly if you need to avoid shadowing.

When to Use Each Type

Use instance variables when each object needs its own independent copy of a value. Examples include a person's name, an account's balance, or a student's grade. These values are specific to that object and should not be shared with other instances.

Use class variables when you need a value that is shared across all instances or that represents a property of the class itself. Examples include a counter tracking how many instances have been created, a configuration setting that applies to all instances, or a constant that defines the class's behavior. Class variables are also useful for storing data that should persist even when no instances currently exist.

Understanding Objects as Instances of Classes

When you write i = 5 in Python, you are creating an object (an instance) of the int class and assigning it to the variable i. The variable i holds a reference to that integer object. Similarly, when you create an instance of a custom class, you are creating an object that is an instance of that class. The class serves as a blueprint or template; the instance is the actual object created from that blueprint. This relationship between class and instance is fundamental to understanding how instance and class variables work together.

A class is a type; an instance is a value of that type. Just as you can have multiple variables of type int (each holding a different integer value), you can have multiple instances of a custom class (each holding different instance variable values). Class variables, however, are properties of the type itself, not of individual values.

Practice

MEDIUM

Design a BankAccount class with the following requirements: Each account should have its own balance (instance variable) and account number (instance variable). The bank wants to track the total number of accounts created (class variable) and the total amount of money across all accounts (class variable). Write out which variables should be instance variables and which should be class variables. Then predict: If you create two accounts with balances of 1000 and 2000, and you modify the total_money class variable to 5000, what will each account see when it accesses total_money?

Hints
  • Think about what is specific to each account versus what belongs to the bank as a whole.
  • Remember that class variables are shared; all instances see the same value.
  • Instance variables are independent; each instance has its own copy.

Summary

Instance variables and class variables are two distinct types of fields in a class. Instance variables belong to individual objects; each instance has its own independent copy. Class variables belong to the class itself and are shared across all instances. Changes to an instance variable affect only that instance; changes to a class variable affect all instances simultaneously. Instance variables are typically defined in the constructor or methods using self; class variables are defined at the class level. Understanding this distinction is essential for designing classes correctly and predicting how your code will behave when you create and modify objects.

Key Takeaways

  • Instance variables belong to individual objects; each instance has its own independent copy of the variable.
  • Class variables belong to the class itself and are shared across all instances; all instances see the same value.
  • Instance variables are typically defined in the constructor or methods; class variables are defined at the class level outside methods.
  • Modifying an instance variable affects only that instance; modifying a class variable affects all instances simultaneously.
  • Use instance variables for data specific to each object and class variables for shared data or properties of the class itself.