why do we use class methods in 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: python classmethod
class point:
def __init__(self, x, y):
self.x = x
self.y = y
@classmethod
def zero(cls):
return cls(0, 0)
def print(self):
print(f"x: {self.x}, y: {self.y}")
p1 = point(1, 2)
p2 = point().zero()
print(p1.print())
print(p2.print())