user1741339
user1741339

Reputation: 515

method overloading python

This is similar to method overloading. When print Hello(1, 2, 3) gets executed it returns "a and b" whereas I want it to return "a and b and c". I know I could have said if (a and b and c is None) then it would have worked. But if I had 20 parameter and I have to handle every case it would be just multiple if statements which I don't think should be necessary. Is there a better way I should be doing such a problem?

 def Hello(a=None, b=None, c=None):
    if(a and b):
        return "a and b"
    if(a and c):
       return "a and c"
    if(a and b and c):
       return "a and b and c"

print Hello(1, 2, 3)

Upvotes: 0

Views: 212

Answers (5)

D3nz13
D3nz13

Reputation: 74

It's not clear what you want to do. The way I've understood it, you want to check which arguments are not None but the number of arguments is not fixed.

You can use *args or **kwargs for that.

def hello(**kwargs):
    return " and ".join((k for k, v in kwargs.items() if v is not None))


>>> hello(a=1, b=2, c=None)  # kwargs = {'a': 1, 'b': 2, 'c': None}
'a and b'
>>> hello(a=1, b=2, c='b')  # kwargs = {'a': 1, 'b': 2, 'c': 'b'}
'a and b and c'

Upvotes: 0

user19921349
user19921349

Reputation:

this is not best but you can use without class(only function)

def my_func(fname=None, lname=None, job=None):
if fname is not None and lname is not None and job is not None:
    print('Hello', fname, lname)
    print('you are', job)
    print("func 1")

elif fname is not None and lname is not None and job is None:
    print('Hello', fname, lname)
    print("func 2")

elif fname is not None and lname is None and job is None:
    print('Hello', fname)
    print("func 3")


  my_func('dev', 'whale', 'programmer')
  my_func('dev', 'whale')
  my_func('dev')

Upvotes: 0

python does not supports method overloading. We may overload the methods but can only use the latest defined method

def product(a, b):
    return a*b

def product(a,b,c):
    return a*b*c

if you call product(10, 20) will give an error

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121436

You need to rethink your program logic, really. Usually, an API will not have all-optional parameters where you need to build a large decision tree from all the inputs.

In other words, in a real program you won't normally encounter a function where the behaviour is completely different for each of the different combinations of inputs. Invariably, the legal combinations is much, much smaller even for a larger number of optional parameters.

For your toy example, you can come up with other ways to create your return value not involving a decision tree:

  • map responses:

    responsemap = {
        (True,  True,  False): "a and b",
        (True,  False, True):  "a and c",
        (True,  True,  True):  "a and b and c"
    }
    
    def Hello(a=None, b=None, c=None):
        return responsemap.get((bool(a), bool(b), bool(c)), None)
    
  • build a string based on the inputs:

    def Hello(a=None, b=None, c=None):
        return ' and '.join([n for n in ('a', 'b', 'c') if locals()[n]])
    

Upvotes: 4

NPE
NPE

Reputation: 500217

You need to reorder the if statements:

 def Hello(a=None, b=None, c=None):
    if a and b and c:
       return "a and b and c"
    if a and b:
        return "a and b"
    if a and c:
       return "a and c"

When a and b and c evaluates to true, a and b also evaluates to true, so in your code this case gets handled by the first if.

Upvotes: 5

Related Questions