1.

Is function overloading supported by Python? Give reasons.

Answer»

A given name can only be associated with one function at a time, so cannot overload a function with multiple definitions. If you define two or more functions with the same name, the last one defined is used. 

However, it is possible to overload a function, or otherwise genericized it. You simply need to create a dispatcher function that then dispatches to your set of corresponding functions. Another way to genericized a function is to make use of the simple generic module which lets you define simple single-dispatch generic

functions.

def test(): #function 1

print “hello”

def test(a, b): #function 2

return a+b

def test(a, b, c): #function 3

return a+b+c

If you run the code of three test functions, the second test() definition will overwrite the first one. Subsequently, the third test() definition will overwrite the second one. That means if you give the function call test (20,20), it will flash an error stating, “Type Error: add() takes exactly 3 arguments (2 givens)”. This is because Python understands the latest definition of the function test() which takes three arguments.



Discussion

No Comment Found