0

I found that I want to modify a recursive function's behavior for a specific input. I know I can do this by rewriting the function, but as the function could be long I want to avoid duplicating code.

As an example, let's say I have implemented the function lambda x: max(x, 10) in the following recursive way:

def orig_fun(x):
  if x < 10:
    return orig_fun(x + 1)
  return x

but now I want to return 20 whenever the input is 5, as in the following

def desired_fun(x):
  if x == 5:
    return 20
  if x < 10:
    return orig_fun(x + 1)
  return x

which is the same as adding an if statement in the begging of orig_fun or writing a new function copying the body of orig_fun. I don't want to do this because the body must be many many lines. Of course, doing new_fun = lambda x: 20 if x == 5 else orig_fun(x) does not work because new_fun(3) would be 3 instead of 20.

Is there a way I can solve this in Python3?

note that this is a duplicate of Extend recursive (library) function without code duplication which has no satisfying answer (some user talked about "hacky ways" not presented)

3
  • 1
    "Of course, doing new_fun = lambda x: 20 if x == 5 else orig_fun(x) does not work because new_fun(3) would be 3 instead of 20." I don't understand that sentence. new_fun(3) would be 10 as expected, not 3 and not 20. Commented Feb 27, 2022 at 14:06
  • If the input is 4 do you want it to return 20? Commented Feb 27, 2022 at 14:13
  • Yes! What is intended is that I can still use the logic of the new function. But as it is, once we enter the recursion we don't access such logic, so even if one intermediate input is 5, we are not returning 20 Commented Feb 28, 2022 at 9:06

1 Answer 1

1

You can use a another function to wrap your main function like that:

def orig_fun(x):
    if x < 10:
        return orig_fun(x + 1)
    return x

def wrapper(x):
    if x == 5:
         return 20
    return orig_fun(x)

>>> print(wrapper(8)) # output: 10
>>> print(wrapper(5)) # output: 20
>>> print(wrapper(12)) # output: 12

update

so you want to change (extend) logic in your recursive function without touching it, then let's make your recursive function non-recursive!

# store orig_fun in another location
main_fun = orig_fun

# re-define orig_fun so it will do one more
# step in every call
def orig_fun(x):
    if x == 5:
        return 20
    return main_fun(x)

>>> orig_fun(2)   # output: 20
>>> orig_fun(5)   # output: 20
>>> orig_fun(7)   # output: 10
>>> orig_fun(12)  # output: 12
>>> orig_fun(35)  # output: 35
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! but that function will return 10 if starting at 4, even if it has 5 as intermediate input. I think it is the same I wrote in the question using lambda notation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.