Skip to content

Class Variables vs Instance Variables in Python

Advertisement

Python, a versatile and widely-used programming language, has two primary types of variables used within classes: class variables and instance variables. Understanding the differences between these two is crucial for effective object-oriented programming in Python.

1. Class Variables

Class variables are shared across all instances of a class. They are defined within the class but outside any methods. Class variables are generally used for properties that should be the same for every instance of a class.

Example:


class Animal:
    kingdom = 'Animalia'  # Class variable

    def __init__(self, name, species):
        self.name = name
        self.species = species

In this example, kingdom is a class variable of the Animal class. It is shared by all instances of Animal. No matter how many animal instances are created, the kingdom will always be ‘Animalia’.

2. Instance Variables

Instance variables are owned by instances of the class. Each object has its own set of instance variables, and they are not shared across instances. Instance variables are defined within methods, most commonly within the __init__ method.

Example:


class Animal:
    kingdom = 'Animalia'  # Class variable

    def __init__(self, name, species):
        self.name = name       # Instance variable
        self.species = species # Instance variable

dog = Animal('Buddy', 'Canine')
cat = Animal('Whiskers', 'Feline')

Here, name and species are instance variables. Each instance of Animal (dog and cat) has its own name and species. Changing name or species in one instance does not affect the others.

3. Differences in Usage

  • Modification: Modifying a class variable affects all instances, while modifying an instance variable affects only that specific instance.
  • Access: Class variables can be accessed using the class name itself or via instances. Instance variables can only be accessed via instances.

4. Practical Example

Let’s see both variables in action:


class Animal:
    kingdom = 'Animalia'  # Class variable

    def __init__(self, name, species):
        self.name = name       # Instance variable
        self.species = species # Instance variable

# Creating instances
dog = Animal('Buddy', 'Canine')
cat = Animal('Whiskers', 'Feline')

# Accessing class variable
print(Animal.kingdom)  # Output: Animalia
print(dog.kingdom)     # Output: Animalia

# Accessing and modifying instance variable
print(dog.name)  # Output: Buddy
dog.name = 'Max'
print(dog.name)  # Output: Max

# The cat instance remains unaffected
print(cat.name)  # Output: Whiskers

In this practical example, modifying dog.name does not affect cat.name, showcasing the independent nature of instance variables. On the other hand, accessing kingdom through both dog and cat yields the same result, underlining the shared nature of class variables.

5. Conclusion

In summary, understanding class and instance variables in Python is essential for designing robust and efficient object-oriented programs. Class variables are ideal for attributes shared by all instances, while instance variables are perfect for attributes unique to each instance. The ‘Animal’ class example aptly demonstrates how these variables can be used and how they behave differently.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top