I'm not referring to closures, where the outer function returns the inner function, or to memoization, specifically. There have been a few instances where I've wanted to write a recursive function, perhaps with memoization, and it seemed much simpler to initialize a dictionary or some other data structures in the outer function, and then have a recursive helper function writing to and accessing the dict and the arguments of the outer function. Here's what I mean --
def foo(arr, l):
cache = {}
result = []
def recursive_foo_helper(i, j, k):
# some code that depends on arr, l, i, j, k, cache, and result
for (x, y) in arr:
if recursive_foo_helper(x, y, k):
return result
return None
instead of having the helper function declared separately with some super long signature like,
recursive_foo_helper(arr, l, i, j, k, cache={}, result=[])
I have read that doing this is pretty standard for memoization, but I was curious if there's a consensus on whether it's OK to do just for recursive helper functions.