How to access Class variables in Python

avatar

Borislav Hadzhiev

Last updated: Sep 14, 2022

banner

Photo from Unsplash

Accessing Class variables in Python #

Access class variables directly on the class, e.g. Employee.cls_variable. Class variables are shared by all instances. 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 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.

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

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 name and salary instance variables are unique to each instance, but the instances share the same cls_id class variable.

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