How to Update or access Class variables in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Updating class variables in Python
  2. Accessing Class variables in Python

# Updating class variables in Python

Update class variables by accessing them directly on the class.

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 code for this article is available on GitHub

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.

# Updating class variables in class methods

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.

# Updating class variables directly on the class

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 a class variable Employee.cls_id = 'new_employee_id' print(Employee.cls_id) # ๐Ÿ‘‰๏ธ new_employee_id

updating class variable directly on the class

The code for this article is available on GitHub

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

# Updating instance variables

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 an 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 instance variables

The code for this article is available on GitHub

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

# Updating a class variable is reflected in all 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 class variable is reflected in all instances

The code for this article is available on GitHub

Updating the cls_id class variable is reflected in all instances.

# Accessing a class variable from an instance of the class

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

accessing class variable from instance of class

The code for this article is available on GitHub

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 sample 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

# Accessing Class variables in Python

Class variables are accessed directly on the class.

You can access instance variables using the self object, e.g. self.name. Instance variables are unique to each instance.

main.py
class Employee(): # ๐Ÿ‘‡๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐Ÿ‘‡๏ธ instance variables self.name = name self.salary = salary def get_name(self): # ๐Ÿ‘‡๏ธ class variable print(Employee.cls_id) # ๐Ÿ‘‡๏ธ access instance variable return self.name bob = Employee('Bobbyhadz', 100) print(bob.get_name()) # ๐Ÿ‘‰๏ธ Bobbyhadz print(bob.cls_id) # ๐Ÿ‘‰๏ธ employee print(Employee.cls_id) # ๐Ÿ‘‰๏ธ employee
The code for this article is available on GitHub

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.

# Accessing class variables by defining a class method

You can also access class variables by defining 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 @classmethod def get_cls_id(cls): return cls.cls_id print(Employee.get_cls_id()) # ๐Ÿ‘‰๏ธ employee
The code for this article is available on GitHub

We marked the get_cls_id method as a class method. The first argument class methods get passed is the class.

Regular methods get passed a self object that gives us a reference to the instance.

main.py
class Employee(): # ๐Ÿ‘‡๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐Ÿ‘‡๏ธ instance variables self.name = name self.salary = salary def get_name(self): print(Employee.cls_id) return self.name alice = Employee('Alice', 200) print(alice.get_name()) # ๐Ÿ‘‰๏ธ Alice print(alice.cls_id) # ๐Ÿ‘‰๏ธ employee bob = Employee('Bobbyhadz', 100) print(bob.get_name()) # ๐Ÿ‘‰๏ธ Bobbyhadz print(bob.cls_id) # ๐Ÿ‘‰๏ธ bob
The code for this article is available on GitHub

The name and salary instance variables are unique to each instance, but the instances share the same cls_id class variable.

# Accessing a class variable from an instance of the class

You can use the type() class if you need to access a class variable from an instance of the class.

main.py
class Employee(): # ๐Ÿ‘‡๏ธ class variable cls_id = 'employee' def __init__(self, name, salary): # ๐Ÿ‘‡๏ธ instance variables self.name = name self.salary = salary def get_name(self): # ๐Ÿ‘‡๏ธ class class variable print(Employee.cls_id) # ๐Ÿ‘‡๏ธ access instance variable return self.name 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 code for this article is available on GitHub

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

Which approach you pick is a matter of personal preference.

The type() class is more elegant because we don't have to access attributes prefixed with two underscores, but the __class__ attribute is more explicit.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev