Static Method in Python

Level 4166 keystrokes

This defines static methods that don't need an instance, printing 7 and 12. Static methods belong to the class, not instances.

Python code

# Define a class to group related utility functions
1class MathUtils:
# @staticmethod decorator creates a method without self parameter
2 @staticmethod
3 def add(a, b):
# Simply returns the sum of two numbers
4 return a + b
5
6 @staticmethod
7 def multiply(a, b):
# Returns the product of two numbers
8 return a * b
9
# Call static methods directly on the class (no instance needed)
10print(MathUtils.add(3, 4))
11print(MathUtils.multiply(3, 4))

Output

7
12
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • (× 6
  • )× 6
  • ,× 4
  • :× 3
  • .× 2
  • @× 2
  • *× 1
  • +× 1

More Python examples at level 4

← Python typing practice