static mehtods python code example
Example 1: static methods in python
class Calculator:
# create addNumbers static method
@staticmethod
def addNumbers(x, y):
return x + y
print('Product:', Calculator.addNumbers(15, 110))
Example 2: staticmethod python
import random
class Example:
# A static method doesn't take the self argument and
# cannot access class members.
@staticmethod
def choose(l: list) -> int:
return random.choice(l)
def __init__(self, l: list):
self.number = self.choose(l)