-1

How can I write multiple functions in python with the same name and a different number of arguments so that function execution is decided by the number of arguments? I am having an error in executing the following program in Python

def area(r):
  a=3.14*r*r
  return a
def area(l,b):
  a=l*b
  return a
print(area(710))
print(area(6,2))
khelwood
  • 55,782
  • 14
  • 81
  • 108
Ash
  • 35
  • 6
  • `def area(x, y=None):`? In the general case this is "multiple dispatch", and you can use libraries like https://pythonhosted.org/multidispatch/. – jonrsharpe Oct 18 '20 at 11:32
  • 1
    Does this answer your question? [How do I use method overloading in Python?](https://stackoverflow.com/questions/10202938/how-do-i-use-method-overloading-in-python) – Abhinav Mathur Oct 18 '20 at 11:46
  • What you are trying to say is to some extent different than what I am asking – Ash Oct 18 '20 at 13:07

2 Answers2

1

In python function overloading is done by providing default values for the parameters For Instance what you can do here is provide a default value for the parameter b (maybe 'None'), and change the rest of the function accordingly

def area(a, b=None):
    if b:
        ans = a*b
    else:
        ans = (a*a)*3.14
    return ans

This function can work if you provide it 2 parameters, the output will be the area of rectangle, or if you give it only parameter to work with it will give back the area of a circle

Proton
  • 35
  • 1
  • 2
  • 8
  • The program which I wrote, I saw it on Unacademy. It was written by one of its teachers'.It worked for her but when I used it for me it showed an error. Can you please explain do I execute the program which I mentioned? – Ash Oct 18 '20 at 11:59
  • "It worked for her" - can't reproduce. – Karl Knechtel Oct 18 '20 at 12:00
  • If I have understood your problem properly, now you just need to call this function like this `print(area(720))` or `print(area(6,2))` that should print the answer for either 1 or 2 parameters. Correct me if you meant something else – Proton Oct 18 '20 at 12:06
  • https://unacademy.com/lesson/function-overloading-in-python-polymorphism/H11OHJJY.This is the link of the video which I saw.The program is between 1:44 and 3:05 – Ash Oct 18 '20 at 12:13
  • @Ash I just checked what you were saying, and I also felt it should work, but it gives a **TypeError** for me too. I can't tell you why its not working, but I think the method I showed you here, is a better choice for overloading in python. – Proton Oct 18 '20 at 12:28
0

I think it's a use case for unpacking.

def area(*values):
    if len(values) == 1:
        r = values[0]
        return 3.14 * r ** 2
    if len(values) == 2:
        l, b = values
        return l * b
    raise ValueError("Takes only one or two arguments.")

In this example, the two asterisks indicates that values will take all positional arguments passed to area. So, if you call area(1), values will be [1], if you call area(1, 2, 3, 4), values will be [1, 2, 3, 4], etc. So you can check the number of arguments passed to the function by checking the length of the list. However, be careful not to overuse it, it can make code a bit confusing...

Dharman
  • 30,962
  • 25
  • 85
  • 135
Michael Marx
  • 104
  • 8