when to use staticmethod python code example
Example 1: static methods in python
class Calculator:
@staticmethod
def addNumbers(x, y):
return x + y
print('Product:', Calculator.addNumbers(15, 110))
Example 2: staticmethod python
import random
class Example:
@staticmethod
def choose(l: list) -> int:
return random.choice(l)
def __init__(self, l: list):
self.number = self.choose(l)
Example 3: static methods in python
class Calculator:
def addNumbers(x, y):
return x + y
Calculator.addNumbers = staticmethod(Calculator.addNumbers)
print('Product:', Calculator.addNumbers(15, 110))