Skip to content

:pythonista: How to write a class in python?

Why we need a class instead of a function?

The main difference between a function and a class is that, using classes we can create multiple instances but using function we can run the same function with different parameters.

Define a class

class YourBusinessLogic:
    def __init__(self):
        """Initializing the class"""
        pass

Methods

def first_logic(self, param1):
    """This is the first business logic created to <purpose>

    :param param1: The parameter to the method
    :type param1: <the data type of the parameter>

    :returns: This function will return the output of the logic
    :rtype: The return type of the function
    """"
    # Your business logic here
    return None

Static method

@staticmethod
def static_method(param1):
    """Static method used to run a specific function while creating the instance
    We can't access any of the class instance using `self` keyword here

    :param param1: The parameter to the method
    :type param1: <the data type of the parameter>

    :returns: This function will return the output of the logic
    :rtype: The return type of the function
    """"
    # Your business logic here
    return None