Consider the code below. It involves application of a very interesting python concept of using default argument during recursion[reversing a list] A = [1,2,3,4]
def recur(A,Q=[]):
if len(A)==1:
Q.append(A[0])
return
recur(A[1:],Q)
Q.append(A[0])
return Q
print(recur(A)) # 4 3 2 1
Issue is, when I call this function again, Q will not be empty. Is there a way I can set Q=[] when this recursive call finishes ?
NOTE: my intent is not to reverse a list, it's just for demonstration. Thanks