instance class and static method in python code example

Example 1: static class python

#To create a static method, just add "@staticmethod" before defining it.

>>>class Calculator:
    # create static method
    @staticmethod
    def multiplyNums(x, y):
        return x * y

>>>print('Product:', Calculator.multiplyNums(15, 110))
Product:1650

Example 2: 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 3: python call static method in class

# Python methods have a __func__ attribute which, when called
# on a staticmethod, allows it to be executed within a class
# body.
class MyClass:
  @staticmethod
  def static_method(n: int) -> int:
    return n
  
  num = static_method.__func__(10) # Call __func__ with argument

mc = MyClass()
print(mc.num) # Output: 10

Example 4: static methods in python

class Calculator:

    def addNumbers(x, y):
        return x + y

# create addNumbers static method
Calculator.addNumbers = staticmethod(Calculator.addNumbers)

print('Product:', Calculator.addNumbers(15, 110))