
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Changing Class Members in Python?
Python object oriented programming allows variables to be used at the class level or the instance level where variables are simply the symbols denoting value you’re using in the program.
At the class level, variables are referred to as class variables whereas variables at the instance level are referred to as instance variables. Let’s understand the class variable and instance variable through a simple example −
# Class Shark class Shark: animal_type= 'fish' # Class Variable def __init__(self, name, age): self.name = name self.age = age # 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) #Let's change the class variable using instance variable obj1.animal_type = "BigFish" print ("\nPrinting class variable after making changes to one instance") print ("obj1.animal_type=", obj1.animal_type) print ("obj2.animal_type =", obj2.animal_type)
In above program, we have created a Shark class and then we are trying to change the class variable using object, this will create a new instance variable for that particular object and this variable shadows the class variable.
output
Printing class variable using two instances obj1.animal_type = fish obj2.animal_type = fish Printing class variable after making changes to one instance obj1.animal_type= BigFish obj2.animal_type = fish
Let’s modify our above program so as to get the correct output −
# Class Shark class Shark: animal_type= 'fish' # Class Variable def __init__(self, name, age): self.name = name self.age = age # 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) #Let's change the class variable using instance variable #obj1.animal_type = "BigFish" Shark.animal_type = "BigFish" print("New class variable value is %s, changed through class itself" %(Shark.animal_type)) print ("\nPrinting class variable after making changes through instances") print ("obj1.animal_type=", obj1.animal_type) print ("obj2.animal_type =", obj2.animal_type)
Result
Printing class variable using two instances obj1.animal_type = fish obj2.animal_type = fish New class variable value is BigFish, changed through class itself Printing class variable after making changes through instances obj1.animal_type= BigFish obj2.animal_type = BigFish
- Related Articles
- Accessing protected members in a C++ derived class
- What are the differences between class methods and class members in C#?
- When are static C++ class members initialized?
- Abstract vs Sealed Classes vs Class Members in C#
- Changing Directions in Python
- How to access the members of a class from another class in Java?
- What are static members of a Java class?
- What are static members of a C# Class?
- C++ Program to Access private members of a class
- Changing ttk Button Height in Python
- What are the steps to read static members in a Java class?\n
- Java Program to Allocate and Initialize Super Class Members using Constructor
- Class Inheritance in Python
- self in Python class
- Why did changing list ‘y’ also change list ‘x’ in Python?

Advertisements