Python Guide Sidebar

Python – Class Attributes

Introduction

In Python, a class defines attributes that are shared across all instances. Unlike instance attributes, which remain unique to each object, class attributes exist within the class but outside any instance methods.These attributes are useful when you need shared data that should be the same across all instances of the class.

1. Defining Class Attributes

A class defines its attributes inside the class but outside any method. All instances of the class share these attributes.

Example:

class Car:
    wheels = 4  # Class attribute

    def __init__(self, brand, model):
        self.brand = brand  # Instance attribute
        self.model = model  # Instance attribute

# Creating instances
car1 = Car("Toyota", "Camry")
car2 = Car("Honda", "Civic")

print(car1.wheels)  # Output: 4
print(car2.wheels)  # Output: 4

Here, wheels is a class attribute, meaning every Car object will have four wheels unless explicitly changed.

2. Accessing Class Attributes

Class attributes can be accessed:

  • Using the class name itself
  • Using an instance of the class

Example:

print(Car.wheels)  # Access using class name
print(car1.wheels)  # Access using an instance

3. Modifying Class Attributes

A. Changing the Class Attribute for All Instances

If you modify the class attribute using the class name, the change applies to all instances.

Car.wheels = 6  # Changing the class attribute

print(car1.wheels)  # Output: 6
print(car2.wheels)  # Output: 6
B. Changing the Class Attribute for a Specific Instance

If an instance tries to modify a class attribute, it creates an instance attribute instead, leaving the class attribute unchanged.

car1.wheels = 5  # Creates a new instance attribute for car1

print(car1.wheels)  # Output: 5 (Instance attribute)
print(car2.wheels)  # Output: 6 (Still using class attribute)
print(Car.wheels)   # Output: 6 (Class attribute remains unchanged)

In this case, car1 now has its own wheels attribute, separate from the class attribute.

4. When to Use Class Attributes?

Class attributes are useful when:

we want a shared value across all instances (e.g., default settings, counters).
You want to define constants inside a class.
You don’t need the attribute to be unique for each instance.

Example – Keeping Track of Objects Created

class Dog:
    count = 0  # Class attribute

    def __init__(self, name):
        self.name = name
        Dog.count += 1  # Incrementing the class attribute

dog1 = Dog("Buddy")
dog2 = Dog("Max")

print(Dog.count)  # Output: 2 (Total instances created)

Here, count is a class attribute that keeps track of how many Dog instances have been created.

5.Difference Between Class Attributes & Instance Attributes

6. Checking Class and Instance Attributes

You can use the __dict__ attribute to inspect whether a variable is a class attribute or instance attribute.

print(car1.__dict__)  # Shows only instance attributes
print(Car.__dict__)   # Shows all class attributes

Conclusion

Class attributes in Python are shared variables that belong to a class rather than any single instance. They are useful when you need a common attribute for all objects. However, if an instance modifies a class attribute, it creates a new instance attribute instead of changing the original class attribute. Understanding this distinction is crucial for writing efficient and structured OOP code.

Additional Resources: