Here are 10 Python code snippets that demonstrate the difference between instance variables and class variables:
1. Basic Example of Instance and Class Variables
classPerson: species ="Human"# Class variabledef__init__(self,name,age):self.name = name # Instance variableself.age = age # Instance variableperson1 =Person("John",25)person2 =Person("Alice",30)print(person1.name, person1.species)# Output: John Humanprint(person2.name, person2.species)# Output: Alice Human
Explanation: species is a class variable shared by all instances, while name and age are instance variables, specific to each instance.
2. Changing Instance and Class Variables
classAnimal: species ="Mammal"# Class variabledef__init__(self,name):self.name = name # Instance variableanimal1 =Animal("Lion")animal2 =Animal("Elephant")animal1.species ="Big Cat"# Modifying instance variableprint(animal1.species)# Output: Big Catprint(animal2.species)# Output: Mammal (unchanged)
Explanation: Modifying species for animal1 creates an instance variable specific to that object, not affecting other instances.
3. Class Variable Shared Across Instances
Explanation: The class variable wheels is shared by all instances of the class, and changing it affects all instances.
4. Class and Instance Variables with Same Name
Explanation: The instance variable type overrides the class variable type within the instance, but the class variable remains unchanged.
5. Accessing Class Variables via Class and Instance
Explanation: Both the class and instances can access class variables, but accessing them via an instance is allowed, though less recommended.
6. Modifying Instance Variable Without Affecting Class Variable
Explanation: Modifying the genre attribute for book1 only changes its instance variable and does not affect book2.
7. Instance Variables in Inherited Classes
Explanation: The inherited sound class variable is shared among all Lion instances, while the name and region are instance variables.
8. Overriding Class Variables in Subclasses
Explanation: Dog overrides the species class variable, and this change only applies to instances of Dog, not the parent Animal.
9. Class Variable Affects All Instances
Explanation: The class variable count is shared across all instances, so both counter1 and counter2 reflect the same value.
10. Instance Variable Default Value
Explanation: The position is an instance variable that can be modified independently for each instance.
Summary of Differences:
Instance Variables are specific to each object created from a class. They are set using self in the __init__ method.
Class Variables are shared across all instances of the class and are set directly in the class body, not using self.
class Person:
type = "Human" # Class variable
def __init__(self, name, type="Homo sapiens"):
self.name = name # Instance variable
self.type = type # Instance variable (overrides class variable)
person = Person("John")
print(person.type) # Output: Homo sapiens (instance variable)
print(Person.type) # Output: Human (class variable)
class Product:
category = "Electronics" # Class variable
def __init__(self, name, price):
self.name = name # Instance variable
self.price = price # Instance variable
product = Product("Laptop", 1200)
print(product.category) # Output: Electronics (accessed through instance)
print(Product.category) # Output: Electronics (accessed through class)
class Animal:
sound = "Roar" # Class variable
def __init__(self, name):
self.name = name # Instance variable
class Lion(Animal):
def __init__(self, name, region):
super().__init__(name)
self.region = region # Instance variable
lion = Lion("Simba", "Africa")
print(lion.name) # Output: Simba
print(lion.region) # Output: Africa
print(Lion.sound) # Output: Roar (inherited class variable)
class Animal:
species = "Mammal" # Class variable
class Dog(Animal):
species = "Canine" # Overriding class variable
dog = Dog()
print(dog.species) # Output: Canine
print(Animal.species) # Output: Mammal