Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Changing Class Members in Python?
Python object-oriented programming allows variables to be used at the class level or the instance level. Class variables are shared among all instances of a class, while instance variables are unique to each object.
Understanding the difference between class and instance variables is crucial when modifying class members. Let's explore how to properly change class variables through examples.
Class vs Instance Variables
Here's a basic example demonstrating class and instance variables ?
# Class Shark
class Shark:
animal_type = 'fish' # Class Variable
def __init__(self, name, age):
self.name = name # Instance Variable
self.age = age # Instance Variable
# Creating objects of Shark class
obj1 = Shark("Jeeva", 54)
obj2 = Shark("Roli", 45)
print("Printing class variable using two instances")
print("obj1.animal_type =", obj1.animal_type)
print("obj2.animal_type =", obj2.animal_type)
Printing class variable using two instances obj1.animal_type = fish obj2.animal_type = fish
Incorrect Way: Creating Instance Variable
When you assign to a class variable through an instance, Python creates a new instance variable that shadows the class variable ?
class Shark:
animal_type = 'fish' # Class Variable
def __init__(self, name, age):
self.name = name
self.age = age
obj1 = Shark("Jeeva", 54)
obj2 = Shark("Roli", 45)
# This creates an instance variable, NOT modifying class variable
obj1.animal_type = "BigFish"
print("After modifying through instance:")
print("obj1.animal_type =", obj1.animal_type) # Instance variable
print("obj2.animal_type =", obj2.animal_type) # Still class variable
print("Shark.animal_type =", Shark.animal_type) # Original class variable
After modifying through instance: obj1.animal_type = BigFish obj2.animal_type = fish Shark.animal_type = fish
Correct Way: Modifying Class Variable
To actually change the class variable for all instances, modify it through the class name ?
class Shark:
animal_type = 'fish' # Class Variable
def __init__(self, name, age):
self.name = name
self.age = age
obj1 = Shark("Jeeva", 54)
obj2 = Shark("Roli", 45)
# Correct way: Change class variable through class name
Shark.animal_type = "BigFish"
print("After modifying class variable:")
print("obj1.animal_type =", obj1.animal_type)
print("obj2.animal_type =", obj2.animal_type)
print("Shark.animal_type =", Shark.animal_type)
After modifying class variable: obj1.animal_type = BigFish obj2.animal_type = BigFish Shark.animal_type = BigFish
Using Class Methods for Safe Modification
A more structured approach is to use class methods for modifying class variables ?
class Shark:
animal_type = 'fish'
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def change_animal_type(cls, new_type):
cls.animal_type = new_type
obj1 = Shark("Jeeva", 54)
obj2 = Shark("Roli", 45)
# Using class method to change class variable
Shark.change_animal_type("Marine Animal")
print("Using class method:")
print("obj1.animal_type =", obj1.animal_type)
print("obj2.animal_type =", obj2.animal_type)
Using class method: obj1.animal_type = Marine Animal obj2.animal_type = Marine Animal
Key Points
| Method | Syntax | Effect |
|---|---|---|
| Instance assignment | obj.class_var = value |
Creates instance variable |
| Class assignment | ClassName.class_var = value |
Modifies class variable |
| Class method | @classmethod |
Structured class variable modification |
Conclusion
To properly change class variables, use the class name directly (ClassName.variable = value) or class methods. Assigning through instances creates instance variables that shadow the class variable, which is usually not the intended behavior.
