Class Attributes in Python


In Python, class attributes are variables that are shared across all instances of a class. Unlike instance attributes, which are unique to each object (instance), class attributes are shared by every instance of the class. Class attributes are usually defined within the class but outside of any instance methods (like __init__).

Key Points about Class Attributes:

  1. Shared by all instances: All instances of the class can access the class attribute and share the same value.

  2. Defined at the class level: Class attributes are defined inside the class but outside of methods.

  3. Accessed through class name or instance: Class attributes can be accessed using both the class name and the instance name, but modifying them via an instance affects only that instance.

Syntax for Class Attributes

  • class ClassName:

  •     class_attribute = value  # Class attribute

  •     

  •     def __init__(self, instance_attribute):

  •         self.instance_attribute = instance_attribute  # Instance attribute


Example of Class Attributes

Let’s create an example with a class called Car that has a class attribute wheels shared by all instances, while each instance has an instance_attribute representing the specific model of the car.

  • class Car:

  •     wheels = 4  # Class attribute

  •     

  •     def __init__(self, model):

  •         self.model = model  # Instance attribute


  • # Creating objects (instances) of the Car class

  • car1 = Car("Toyota")

  • car2 = Car("Honda")


  • # Accessing class attribute through the class name

  • print(Car.wheels)  # Output: 4


  • # Accessing class attribute through an instance

  • print(car1.wheels)  # Output: 4

  • print(car2.wheels)  # Output: 4


  • # Accessing instance attribute

  • print(car1.model)  # Output: Toyota

  • print(car2.model)  # Output: Honda


In this example:

  • wheels is a class attribute, and it’s shared across all instances of the Car class.

  • Each car object (car1 and car2) has a unique model attribute.

  • You can access the class attribute through both the class name (Car.wheels) and the instance (car1.wheels).

Modifying Class Attributes

You can modify class attributes directly through the class name or by accessing it through an instance. However, modifying the class attribute through an instance can lead to unexpected behavior, as it may create an instance attribute with the same name.

Modifying via Class Name

  • class Car:

  •     wheels = 4

  •     

  •     def __init__(self, model):

  •         self.model = model


  • # Creating an object

  • car1 = Car("Toyota")


  • # Modifying the class attribute using the class name

  • Car.wheels = 6


  • # Accessing the modified class attribute through instances

  • print(car1.wheels)  # Output: 6


Modifying via Instance (creates instance attribute)

  • car2 = Car("Honda")

  • car2.wheels = 8  # This creates an instance attribute 'wheels' for car2


  • # Accessing the instance and class attributes

  • print(car2.wheels)  # Output: 8 (instance attribute, not the class attribute)

  • print(Car.wheels)   # Output: 6 (class attribute is still 6)


In this case, car2.wheels = 8 modifies the wheels attribute for that specific instance, not the class attribute. The class attribute remains unchanged at 6 for other instances, like car1.

Use Case for Class Attributes

Class attributes are useful when you want to store information that is common to all instances of a class. For example, a class attribute can be used to keep track of the number of instances created from a class, or any constant value shared by all objects of the class.

Example: Tracking Instance Count with Class Attribute

  • class Animal:

  •     count = 0  # Class attribute to track number of animals

  •     

  •     def __init__(self, name):

  •         self.name = name

  •         Animal.count += 1  # Increment the count every time an object is created


  • # Creating instances of Animal

  • dog = Animal("Dog")

  • cat = Animal("Cat")


  • print(f"Total animals created: {Animal.count}")  # Output: Total animals created: 2


In this example:

  • count is a class attribute that keeps track of how many animals have been created.

  • Every time a new Animal instance is created, the count is incremented.

Conclusion

Class attributes are shared among all instances of a class, making them ideal for storing values that are common across instances. While instance attributes are specific to each object, class attributes are shared by all objects created from the class. You can modify class attributes directly via the class name, and they are accessed by both the class and instances.

Let me know if you need more details or further examples! 😊


Post a Comment

0 Comments