Topics

static Keyword

Welcome to a tutorial on Static Variables and Methods in Python. In programming, defining static variables and method is a common concept and is widely used in languages like Java, C++, PHP, and so on. They are used in creating class variables and methods that belong to the class and are shared by all the objects of the class.

There is no special keyword for creating static variables and methods in Python. However, Python follows a different but simple approach for defining static variables and methods. You will get to learn that in this tutorial.

 

Class or Static Variables in Python

In Python programming language, Class or Static variables are the variables that belong to the class, but not to objects. They are shared amongst objects of the class. Every variable that is assigned a value in the class declaration is a class variable. Also, variables that are assigned values inside class methods are regarded as instance variables. 

Check out the example below:

class Shape:
    # class or static variable 
    cat = 'Geometrical'
    
    def __init__(self, type):
        # instance variable
        self.typ = type
    
    # method to show data
    def show(self):
        # accessing class variable
        print('Shape is of category: ', Shape.cat)
        # accessing instance variable
        print('And shape is: ', self.type)


tr = Shape('Triangle')
sq = Shape('Square')
rec = Shape('Circle')

tr.show()
sq.show()
rec.show()

 

From the above example, the cat is a class variable because it is defined outside of all the class methods and inside the class definition; the type is an instance variable defined inside a method.

Furthermore, this was confirmed by using the print statement in which the cat variable is referenced using the class name Shape, and the type variable is referenced using the different object references.

The example above illustrates a situation in which there are different shapes of objects, where each belongs to the same category that is Geometrical, however, they are of different types, and therefore, each of the classes has the same category which we have made the class variable and the type variable is different for all the objects, thus, it is an instance variable.

Take note that Python permits the provision of the same variable name for a class/static variable and an instance variable. However, it is recommended that you do not provide the same name variables to these variables to avoid confusion or conception

 

Static Methods in Python

This is quite similar to static variables, as static methods are the methods that are bound to the class rather than an object of the class and therefore, are called when using the class name and not the objects of the class.

Since static methods are bound to the class, they cannot change the state of an object.

In addition, in other to call a static method we don't need any class object because it can be called directly by using the class name.

There are ways of defining a static method in python.

  • Making use of the staticmethod()
  • Making use of the @staticmethod

 

Define static method using staticmethod()

Check out the example below:

class Shape:
    
    def info(msg):
        # show custom message
        print(msg)
        print("This class is used for representing different shapes.")
        

# create info static method
Shape.info = staticmethod(Shape.info)

Shape.info("Welcome to Shape class")

From the code above, the method info was declared as a static method outside the class using the staticmethod() function approach, thereafter we were able to call the info() method directly using the class Shape.

 

Define static method using @staticmethod

Check out the example below:

class Shape:
    
    @staticmethod
    def info(msg):
        # show custom message
        print(msg)
        print("This class is used for representing different shapes.")

Shape.info("Welcome to Shape class")

More importantly, using the @staticmethod is a more modern approach to defining static method, thereby, we recommend this approach.

Points to Remember about static variables and methods

 

Important things to know:

  • Static variables and methods can be used when we want to define some behavior or property specific to the class and it is something common for all the class objects.
  • With close observation, for a static method, we don't provide the argument self because static methods do not operate on objects.