I am trying to emulate a switch-case statement from the validate_input2
function below.
def _validate_inputs2(*args):
if len(args) == 1:
stop = args
start = 1
step = 1
elif len(args) == 2:
start, stop = args
step = 1
elif len(args) == 3:
start, stop, step = args
else:
raise TypeError("xxx expected at most 3 arguments, got 4")
if 0 == step:
raise ValueError("xxx arg 3 must not be zero")
return start, stop, step
This is what I basically did but it doesn't work correctly
def _validate_inputs(*args):
start, stop, step = {
len(args) == 1: lambda x, y, z: (args, 1, 1),
len(args) == 2: lambda x, y, z: (args, 1),
len(args) == 3: args
}.get(args, lambda: TypeError("xxx expected at most 3 arguments, got 4"))()
if 0 == step:
raise ValueError("xxx arg 3 must not be zero")
return start, stop, step
Even though I find this emulation less readable I would like to better understand it in order to improve my python skills.
Can someone help me simplify this code?