I have been looking for the answer to this for sometime but I always end up looking at tutorials covering the basics. My problem is -
If I have a function:
def foo(a, b, *args, **kwargs):
if args[0]:
if args[0] in range(1, 4):
x=args[0]
else:
raise ValueError("Eww that is one ugly x!")
else:
x = kwargs.get('x', 3)
if args[1]:
if args[1] in ['some','list','of','strings']:
y = args[1]
else:
raise ValueError("Invalid y")
else:
y = kwargs.get('y', "some")
if x == 1:
print("Good")
elif x == 2:
print("Bad")
elif x == 3:
print("Clint")
else:
raise ValueError("Eww that is one ugly x!")
if y == 'some':
print(y + str(x))
elif y == 'list':
print("happy")
elif y == 'of':
print("healthy")
elif y == 'strings':
print(y + 'me')
else:
raise ValueError("Invalid y")
I am looking for a simpler way of treating args[0] and kwargs.get('x') as equivalent - insofar as I would like to perform the same type and value validation checks on which ever is assigned. In short how do I map the value of both args[i] and kwargs.get(k) to the same object.