First page Back Continue Last page Overview Graphics

Static Methods

Static methods don't need a reference to the instance, i.e. the self!

Static methodes are defined to demonstrate that a method doesn't depend on the state of an instance!

Static methods could be in principle defined outside of the class, but this would mean that they can't be overridden by an inheriting class.

class Robot:

@staticmethod

def hi():

print("Hi")

x = Robot()

Robot.hi()

x.hi()

Hi

Hi

Output

Without decorating hi with @staticmethod

We would raise an exception by calling „x.hi()“.