Static method can be called without creating an object or instance. Simply create the method and call it directly.

This is in a sense orthogonal to object orientated programming: we call a method without creating objects.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

Example

Demonstration of static method below. Define a class with a method. Add the keyword @staticmethod above it to make it static.

class Music:
@staticmethod
def play():
print("*playing music*")

Music.play()

Static-methods inside a class

A class can contain both static and non-static methods. If you want to call non-static methods, you’ll have to create an object. The code below does not work because an object is not created:

class Music:
@staticmethod
def play():
print("*playing music*")

def stop(self):
print("stop playing")

Music.play()
Music.stop()

Calling static methods

If we create an object, we can call non-static methods:

class Music:
@staticmethod
def play():
print("*playing music*")

def stop(self):
print("stop playing")

Music.play()

obj = Music()
obj.stop()