As functions in Python are objects so we can pass them into other functions. For example:
def hello(x) :
return "Hello World"*x
def bye(x) :
return "Bye World"*x
def analyze(func,x) :
return func(x)
For analyze(bye, 3)
OUTPUT is Bye WorldBye WorldBye World
For analyze(hello, 3)
OUTPUT is Bye World WorldHello World
It makes sense but while doing the same in-class Objects it throws an error. For example:
class Greetings:
def __init__(self):
pass
def hello(self, x) :
return "Hello World"*x
def bye(self, x) :
return "Bye World"*x
def analyze(self, func, x) :
return self.func(x)
Driver Code:
obj = Greetings()
obj.analyze(hello, 3)
Throws TypeError: analyze() missing 1 required positional argument: 'x'
I even tried obj.analyze(obj, hello, 3)
Then it throws AttributeError: type object 'Greetings' has no attribute 'func'
exception.