How can I declare a few methods with the same name, but with different numbers of parameters or different types in one class?
What must I change in the following class?>
You can have a function that takes in a variable number of arguments.
def my_method(*args, **kwds):
# Do something
# When you call the method
my_method(a1, a2, k1=a3, k2=a4)
# You get:
args = (a1, a2)
kwds = {'k1':a3, 'k2':a4}
So you can modify your function as follows:
def my_method(*args):
if len(args) == 1 and isinstance(args[0], str):
# Case 1
elif len(args) == 2 and isinstance(args[1], int):
# Case 2
elif len(args) == 2 and isinstance(args[1], str):
# Case 3