Updating class variables in Python

avatar

Borislav Hadzhiev

Last updated: Sep 14, 2022

banner

Photo from Unsplash

Updating class variables in Python #

Update instance variables by accessing them directly on the class, e.g. Employee.cls_variable = new_value. Class variables are shared by all instances and can be updated directly on the class or in a class method.

main.py
class Employee(): # 👇️ class variable cls_id = 'employee' def __init__(self, name, salary): # 👇️ instance variables self.name = name self.salary = salary # ✅ update class variable @classmethod def set_cls_id(cls, new_cls_id): cls.cls_id = new_cls_id return cls.cls_id # ✅ update instance variable def set_name(self, new_name): self.name = new_name return new_name print(Employee.cls_id) # 👉️ employee Employee.set_cls_id('new_employee_id') print(Employee.cls_id) # 👉️ new_employee_id bob = Employee('Bobbyhadz', 100) bob.set_name('Bobbyhadz2') print(bob.name) # 👉️ Bobbyhadz2

The class has a cls_id class variable and name and salary instance variables.

Class variables are shared by all instances and can be accessed directly on the class, e.g. Employee.cls_id.

Instance variables are unique to each instance you create by instantiating the class.

We used a class method to update the cls_id variable.

main.py
@classmethod def set_cls_id(cls, new_cls_id): cls.cls_id = new_cls_id return cls.cls_id

Class methods get passed the class as the first argument.

Note that you can also set class variables directly on the class, e.g. Employee.cls_id = new_cls_id.

main.py
class Employee(): # 👇️ class variable cls_id = 'employee' def __init__(self, name, salary): # 👇️ instance variables self.name = name self.salary = salary print(Employee.cls_id) # 👉️ employee # ✅ update class variable Employee.cls_id = 'new_employee_id' print(Employee.cls_id) # 👉️ new_employee_id

Class variables are shared by all instances of the class, whereas instance variables are unique to each instance.

You'll more often have to update instance variables in the class. Here is an example.
main.py
class Employee(): # 👇️ class variable cls_id = 'employee' def __init__(self, name, salary): # 👇️ instance variables self.name = name self.salary = salary # ✅ update instance variable def set_name(self, new_name): self.name = new_name return new_name alice = Employee('Alice', 150) bob = Employee('Bobbyhadz', 100) bob.set_name('Bobbyhadz2') print(bob.name) # 👉️ Bobbyhadz2 print(alice.name) # 👉️ Alice

Updating one instance variable doesn't update the attribute for the other instances.

On the other hand, when you update a class variable, the value is updated for all instances.

main.py
class Employee(): # 👇️ class variable cls_id = 'employee' def __init__(self, name, salary): # 👇️ instance variables self.name = name self.salary = salary alice = Employee('Alice', 150) bob = Employee('Bobbyhadz', 100) print(bob.cls_id) # 👉️ employee Employee.cls_id = 'NEW_ID' print(alice.cls_id) # 👉️ NEW_ID print(bob.cls_id) # 👉️ NEW_ID

Updating the cls_id class variable is reflected in all instances.

You can use the type() class if you need to access a class variable from an instance of the class.
main.py
class Employee(): cls_id = 'employee' def __init__(self, name, salary): self.name = name self.salary = salary bob = Employee('Bobbyhadz', 100) # 👇️ override class variable on the instance bob.cls_id = 'new' print(bob.cls_id) # 👉️ new # 👇️ access the actual class variable from the instance result = type(bob).cls_id print(result) # 👉️ employee

The instance overrides the cls_id variable, so to access the actual class variable, we had to use the type() class.

The type class returns the type of an object.

Most commonly the return value is the same as accessing the __class__ attribute on the object.

The following code snippet uses the __class__ attribute and achieves the same result.

main.py
bob = Employee('Bobbyhadz', 100) bob.cls_id = 'new' print(bob.cls_id) # 👉️ new result = bob.__class__.cls_id print(result) # 👉️ employee
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.