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)
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:
build a string based on the inputs: