Accessing static class variables in Python

avatar

Borislav Hadzhiev

Last updated: Sep 14, 2022

banner

Photo from Unsplash

Accessing static class variables in Python #

You can access static class variables directly on the class, e.g. Employee.static_variable. Variables declared inside of the class definition, but outside of methods are called static or class variables and are shared by all instances of the class.

main.py
class Employee(): # 👇️ static class variable cls_id = 'employee' def __init__(self, name, salary): # 👇️ instance variables self.name = name self.salary = salary def get_name(self): # 👇️ static 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 static variable and name and salary instance variables.

Static variables are more commonly called class variables in Python.

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.

Notice that static variables can also be accessed on instances of the class.

There is also a @staticmethod decorator which transforms a method into a static method.

A static method doesn't receive an implicit first argument.

main.py
class Employee(): # 👇️ static class variable cls_id = 'employee' def __init__(self, name, salary): # 👇️ instance variables self.name = name self.salary = salary @staticmethod def example_static_method(): print(Employee.cls_id) Employee.example_static_method() # 👉️ employee bob = Employee('Bobbyhadz', 100) bob.example_static_method() # 👉️ employee

A static method can be called on the class or an instance of the class.

However, static methods are mostly used for organization purposes, so you won't have to use them.

You can also access static (class) variables by defining a class method.

main.py
class Employee(): # 👇️ static 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 bob = Employee('Bobbyhadz', 100) print(bob.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.

You can call class methods directly on the class or on an instance of 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', 150) 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(): cls_id = 'employee' def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): print(Employee.cls_id) 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

Note that you won't often see the term "static variable" used in Python.

Static variables are more commonly called class variables and are shared by all instances of the class.

On the other hand, instance variables are unique to each instance you create by instantiating the class.

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.