I am writing an auxiliary python function that takes as arguments two lists and iterates over them. Often the second list needs to be an array of zeros with the same length as the first. I would like to set up the second list as a keyword argument and assign it this default value.
def iterate(list_a, list_b = np.zeros((len(list_a),), dtype=int)):
for a, b in zip(list_a, list_b):
# do something with a and b
However, I get an error that list_a is not defined, suggesting I can't perform self-referencing computations in the brackets when defining a function. An obvious solution is to pick a special default value for list_b and, if left unchanged when calling the function, change it to a list of zeros with an if statement:
def iterate(list_a, list_b = 'zeros'):
if list_b == 'zeros':
list_b = np.zeros((len(list_a),), dtype=int)
for a, b in zip(list_a, list_b):
# do something with a and b
This solution doesn't seem very pythonic to me and I am wondering if there is a better practice for going about it.
I am keeping it general on purpose but I can give more detail about what my function does if needed.
arg=Noneused and if it still none, default value initialized.Noneas the default arg. You're correct in that default arg cannot reference another arg in the way you'd want it to work.