What is the correct way to define class variables in Python?

Class variables are variables that are declared outside the __init__ method. These are static elements, meaning they belong to the class rather than to the class instances. These class variables are shared by all instances of that class.

Basic Syntax

Class variables are defined directly inside the class body, outside any method ?

class MyClass:
    class_var1 = 123
    class_var2 = "abc"
    
    def __init__(self):
        # Instance variables go here
        pass

Example: Class vs Instance Variables

Here's a complete example showing how class variables are shared among all instances ?

class MyClass:
    stat_elem = 456  # Class variable
    
    def __init__(self):
        self.object_elem = 789  # Instance variable

# Create two instances
c1 = MyClass()
c2 = MyClass()

# Initial values of both elements
print("c1:", c1.stat_elem, c1.object_elem)
print("c2:", c2.stat_elem, c2.object_elem)
c1: 456 789
c2: 456 789

Modifying Class Variables

When you modify a class variable through the class name, it affects all instances ?

class MyClass:
    stat_elem = 456
    
    def __init__(self):
        self.object_elem = 789

c1 = MyClass()
c2 = MyClass()

# Change the class variable
MyClass.stat_elem = 888

print("After changing class variable:")
print("c1:", c1.stat_elem, c1.object_elem)
print("c2:", c2.stat_elem, c2.object_elem)
After changing class variable:
c1: 888 789
c2: 888 789

Modifying Instance Variables

Instance variables only affect the specific instance they belong to ?

class MyClass:
    stat_elem = 456
    
    def __init__(self):
        self.object_elem = 789

c1 = MyClass()
c2 = MyClass()

# Change instance variable for c1 only
c1.object_elem = 777

print("After changing instance variable:")
print("c1:", c1.stat_elem, c1.object_elem)
print("c2:", c2.stat_elem, c2.object_elem)
After changing instance variable:
c1: 456 777
c2: 456 789

Best Practices

Here are some guidelines for defining class variables correctly ?

class Student:
    # Public class variable
    school_name = "ABC High School"
    
    # Private class variable (convention)
    _total_students = 0
    
    def __init__(self, name):
        self.name = name  # Instance variable
        Student._total_students += 1
    
    @classmethod
    def get_total_students(cls):
        return cls._total_students

# Usage
s1 = Student("Alice")
s2 = Student("Bob")

print("School:", Student.school_name)
print("Total students:", Student.get_total_students())
School: ABC High School
Total students: 2

Key Points

  • Class variables are shared by all instances of the class
  • Define them directly in the class body, outside methods
  • Access through the class name or instance
  • Use underscore prefix for private class variables by convention
  • Modify through the class name to affect all instances

Conclusion

Class variables should be defined directly in the class body outside any methods. They are shared among all instances and are ideal for storing data that belongs to the class as a whole rather than individual instances.

Updated on: 2026-03-24T19:38:07+05:30

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements