Instance vs Class Variables in Python – Differences & Examples

Introduction

Instance variables and class variables are used to store data within a class, but they serve different purposes and behave differently.

Many beginners get confused because both types of variables are accessed through objects, yet they store data in different ways.

For example, consider a school management system:

  • Every student has a unique name and roll number.
  • All students belong to the same school.

In this case:

  • Name and roll number should be instance variables because they vary from student to student.
  • School name should be a class variable because it is shared among all students.

What are Variables in a Class?

Variables inside a class are used to store data related to objects or the class itself.

Python mainly provides two types of variables:

  1. Instance Variables
  2. Class Variables

Both are important in Object-Oriented Programming.

What are Instance Variables?

Instance variables are variables that belong to an individual object.

Each object gets its own separate copy of instance variables.

They are usually defined inside the __init__() constructor using the self keyword.

Syntax


class Student:
    def __init__(self, name):
        self.name = name

Here:

self.name is an instance variable.

Example of Instance Variables


class Student:
    def __init__(self, name):
        self.name = name
student1 = Student("John")
student2 = Student("Emma")
print(student1.name)
print(student2.name)

Output:

John
Emma

Explanation:

  • student1 stores “John”
  • student2 stores “Emma”
  • Each object has its own value

Characteristics of Instance Variables

1. Unique for Each Object

Each object stores separate values.

2. Defined Using self


self.name

3. Created During Object Creation

Usually initialized inside the constructor.

4. Object-Specific Data

Store information that differs among objects.

Examples:

  • Student name
  • Employee salary
  • Product price
  • Customer email

What are Class Variables?

Class variables are variables shared by all objects of a class.

They are declared directly inside the class but outside methods.

Syntax


class Student:
    school = "ABC School"

Here: school is a class variable.

Example of Class Variables


class Student:
    school = "ABC School"
student1 = Student()
student2 = Student()
print(student1.school)
print(student2.school)

Output:

ABC School
ABC School

Explanation:

Both objects share the same class variable.

Characteristics of Class Variables

1. Shared Across Objects

One copy exists for the entire class.

2. Defined Outside Methods


class Student:
    school = "ABC School"

3. Memory Efficient

All objects use the same variable.

4. Stores Common Data

Examples:

  • Company name
  • School name
  • Tax rate
  • Country code

Instance Variables vs Class Variables

Let’s understand the difference through an example.


class Student:
    school = "ABC School"
    def __init__(self, name):
        self.name = name
student1 = Student("John")
student2 = Student("Emma")
print(student1.name)
print(student2.name)
print(student1.school)
print(student2.school)

Output:

John
Emma
ABC School
ABC School

Explanation:

Instance Variable:


self.name

Class Variable:


school

Detailed Comparison Table

Feature Instance Variable Class Variable
Belongs To Object Class
Copy Count Separate for each object Shared among all objects
Defined Inside Constructor or methods Class body
Accessed Using self.variable ClassName.variable
Memory Usage More Less
Stores Object-specific data Shared data
Modified By Individual object Class or object

Accessing Instance Variables


class Employee:
    def __init__(self, name):
        self.name = name
employee = Employee("David")
print(employee.name)

Output:

David

Accessing Class Variables

Using Object


class Employee:
    company = "ABC Ltd"
employee = Employee()
print(employee.company)

Output:

ABC Ltd

Using Class Name


class Employee:
    company = "ABC Ltd"
print(Employee.company)

Output:

ABC Ltd

Using the class name is generally preferred.

Modifying Instance Variables

Instance variables affect only one object.


class Student:
    def __init__(self, name):
        self.name = name
student1 = Student("John")
student2 = Student("Emma")
student1.name = "Michael"
print(student1.name)
print(student2.name)

Output:

Michael
Emma

Explanation:

Only student1 changes.

Modifying Class Variables

Class variables affect all objects.


class Student:
    school = "ABC School"
Student.school = "XYZ School"
student1 = Student()
student2 = Student()
print(student1.school)
print(student2.school)

Output:

XYZ School
XYZ School

Explanation:

Changing the class variable updates it for every object.

What Happens When an Object Modifies a Class Variable?

Consider:


class Student:
    school = "ABC School"
student1 = Student()
student2 = Student()
student1.school = "XYZ School"
print(student1.school)
print(student2.school)

Output:

XYZ School
ABC School

Explanation:

Python creates a new instance variable for student1.

The original class variable remains unchanged.

Real-Life Example: School Management System


class Student:
    school = "ABC School"
    def __init__(self, name, roll_no):
        self.name = name
        self.roll_no = roll_no
student1 = Student("John", 101)
student2 = Student("Emma", 102)
print(student1.name)
print(student2.name)
print(student1.school)
print(student2.school)

Output:

John
Emma
ABC School
ABC School

Instance Variables:

  • name
  • roll_no

Class Variable:

  • school

Real-Life Example: Employee Management System


class Employee:
    company = "Tech Solutions"
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary
employee1 = Employee(
    "David",
    50000
)
employee2 = Employee(
    "Sarah",
    70000
)
print(employee1.company)
print(employee2.company)

Output:

Tech Solutions
Tech Solutions

Explanation:

The company name is shared among all employees.

Advantages of Instance Variables

1. Store Unique Data

Every object can have different values.

2. Flexible

Objects can maintain independent states.

3. Realistic Modeling

Represent real-world entities effectively.

4. Easy Customization

Modify one object’s data without affecting others.

Advantages of Class Variables

1. Memory Efficient

Only one copy exists.

2. Shared Information

Perfect for common data.

3. Easy Updates

One change affects all objects.

4. Better Organization

Centralizes common values.

Common Mistakes

1. Using Class Variables for Unique Data

Incorrect:


class Student:
    name = ""

All objects share the same variable.

Better:


class Student:
    def __init__(self, name):
        self.name = name

2. Defining Class Variables Inside init()

Incorrect:


def __init__(self):
    self.school = "ABC School"

This creates an instance variable.

Correct:


class Student:
    school = "ABC School"

3. Modifying Class Variables Through Objects


student.school = "XYZ School"

This creates an instance variable instead of updating the class variable.

Preferred:


Student.school = "XYZ School"

4. Forgetting self for Instance Variables

Incorrect:


name = "John"

Correct:


self.name = "John"

5. Accessing Instance Variables Without Objects

Incorrect:


print(Student.name)

unless it is a class variable.

Best Practices

1. Use Instance Variables for Unique Data


self.name
self.salary
self.email

2. Use Class Variables for Shared Data


company_name
school_name
tax_rate

3. Access Class Variables Using Class Name

Modify them through the class rather than objects.

4. Use Meaningful Names

Good:


self.student_name

Bad:


self.x

Conclusion

Understanding the difference between instance variables and class variables is essential for effective Object-Oriented Programming in Python.

Instance variables store object-specific data and provide flexibility for individual objects, while class variables store shared data that is common across all objects of a class.

Related Python Tutorials