I do not mean closure where an external function returns, in particular, an internal function or memoization. There were several cases where I wanted to write a recursive function, possibly with memoization, and it was much easier to initialize a dictionary or some other data structures in an external function, and then have a recursive helper function, access to the dict, and arguments to the external function. Here is what I mean -
def foo(arr, l): cache = {} result = [] def recursive_foo_helper(i, j, k):
instead of having a helper function declared separately with some super long signature like
recursive_foo_helper(arr, l, i, j, k, cache={}, result=[])
I read that this is pretty standard for memoization, but I was curious if there was a consensus as to whether this should be done only for recursive helper functions.
source share