Imagine that there is a function g
I want to implement by chaining sub-functions. This can be easily done by:
def f1(a):
return a+1
def f2(a):
return a*2
def f3(a):
return a**3
g = lambda x: f1(f2(f3(x)))
However, now consider that, which sub-functions will be chained together, depends on conditions: specifically, user-specified options which are known in advance. One could of course do:
def g(a, cond1, cond2, cond3):
res = a
if cond1:
res = f3(res)
if cond2:
res = f2(res)
if cond3:
res = f1(res)
return res
However, instead of dynamically checking these static conditions each time the function is called, I assume that it's better to define the function g
based on its constituent functions in advance.
Unfortunately, the following gives a RuntimeError: maximum recursion depth exceeded
:
g = lambda x: x
if cond1:
g = lambda x: f3(g(x))
if cond2:
g = lambda x: f2(g(x))
if cond3:
g = lambda x: f1(g(x))
Is there a good way of doing this conditional chaining in Python? Please note that the functions to be chained can be N, so it's not an option to separately define all 2^N function combinations (8 in this example).
g = lambda x: f(g(x))
will blow up your stack because the tail call never ends. – Kovnof1, f2, ...fN
or will you put the into adict
or something? I'm asking because this will pretty much define how to chain them efficiently. – Vestmentg
as a function object defined previously in the code – Halflengthg = lambda x, g=g: f3(g(x))
(the default parameter captures the previous value ofg
, rather than recursively referring to the new value). – Malinin