Class attribute in Python

In Python, a class attribute is a variable that is defined inside a class but outside of any instance methods.

It is shared across all instances (objects) of that class.

This means:

Syntax

class ClassName:
    class_variable = value   # class variable

    def __init__(self, attribute):
        self.attribute = attribute   # instance variable 

Example:

Python

class Student:
    school_name = "ABC Public School"   # class variable (shared)

    # Constructor
    def __init__(self, name):
        self.name = name                # instance variable

# Create two objects
s1 = Student("Ashish")
s2 = Student("Katrina")

# Change class variable for s1 only
s1.school_name = "XYZ International School"

print(s1.name, "-", s1.school_name)  # Output: Ashish - XYZ International School

# Access class variable using object name s2
print(s2.name, "-", s2.school_name)  # Output: Katrina - ABC Public School

# Access class variable using class name Student
print(Student.school_name)           # Output: ABC Public School 

What Happened?

  1. Student.school_name → Class variable (shared by all).
  2. s1.school_name = "XYZ International School" → Created a new instance variable named school_name only in s1.
  3. Now:
    • s1.school_name → Refers to its own instance variable.
    • s2.school_name → Still uses the class variable.

Accessing Class Attributes in Python Accessed with either ClassName.attribute or object.attribute.

1. Using the class name (recommended for clarity): ClassName.ClassVariableName

2. Using an object (instance) of the class: object.ClassVariableName

Example:

Python

class Car:
    wheels = 4  # class attribute

# Accessing using class name
print(Car.wheels)   # ✅ 4 (clear and recommended)

# Accessing using an object
car1 = Car()
print(car1.wheels)  # ✅ 4 (also works, but less explicit)

Modifying Class Attributes in Python A class attribute can be modified in two ways:

1. Modify using the class name (affects all instances that don’t override it):

Python

class Car:
    wheels = 4   # class attribute

car1 = Car()
car2 = Car()

print(car1.wheels, car2.wheels)  # 4 4

# Modify class attribute using class name
Car.wheels = 6

print(car1.wheels, car2.wheels)  # 6 6  (both updated) 

2. Modify using an object (creates an instance attribute instead):

Python

class Car:
    wheels = 4

car1 = Car()
car2 = Car()

# Modify through instance
car1.wheels = 8   # This does NOT change the class attribute
                  # It creates a new instance attribute for car1 only

print(car1.wheels)  # 8  (instance attribute shadows class attribute)
print(car2.wheels)  # 4  (still from class attribute)
print(Car.wheels)   # 4  (class attribute unchanged) 

Key Notes • ✅ Use ClassName.attribute to modify class attribute for all objects.
• ⚠️ Using object.attribute = value will not modify the class attribute, it only creates/updates an instance attribute.

Deleting an Instance Attribute We can use the del keyword to delete an instance attribute.
After deletion, the object will look up the attribute in the class again.

Example:

Python

class Car:
    wheels = 4   # class attribute

car1 = Car()
car2 = Car()

# Override class attribute with an instance attribute
car1.wheels = 8
print(car1.wheels)  # 8 (instance attribute)
print(car2.wheels)  # 4 (class attribute)
print(Car.wheels)   # 4 (class attribute)

# Delete the instance attribute from car1
del car1.wheels

# Now car1 falls back to the class attribute
print(car1.wheels)  # 4 (back to class attribute) 

Key Points • Before del: car1.wheels → 8 (instance attribute shadows class attribute). • After del: car1.wheels → 4 (falls back to class attribute).

Summary: