DEV Community

Phansivang
Phansivang

Posted on

Python different between Function and OOP (class and object)

Function

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


def multi(x, y):
    return x * y


print(add(20, 200))
print(multi(22, 40))
Enter fullscreen mode Exit fullscreen mode

Class and object (OOP)

class do_match:
    def __init__(self, val1, val2):
        self.val1 = val1
        self.val2 = val2

    def add(self):
        return self.val1 + self.val2

    def multi(self):
        return self.val1 * self.val2


ob = do_match(2, 40)
print(ob.multi())
print(ob.add())

Enter fullscreen mode Exit fullscreen mode

*Which one is easier and faster? *

Top comments (0)