How to simulate function overload with different number of parameters in Python

2 Answers

0 votes
def add(n, m=None):
    if m:
        return n + m
    else:
        return n + 1

print(add(2))
print(add(3, 4))


'''
run:

3
7

'''

 



answered Feb 11, 2018 by avibootz
0 votes
def add(*n):
    if len(n) == 1:
        return n[0] + 1
    else:
        return n[0] + n[1]
    
print(add(2))
print(add(3, 4))


'''
run:

3
7

'''

 



answered Feb 12, 2018 by avibootz
...