DRYly并且巧妙地编写一个递归函数

时间:2015-03-25 23:53:29

标签: python recursion dry

我正在写一个看起来像这样的递归函数:

def mah_recursive_function(some_arg, some_option=True):
    if some_thing:
        some_arg.doFancyStuff();
        mah_recursive_function(some_arg,
                               some_option=some_option
        )
    elif some_thing:
        some_arg.doOtherFancyStuff();
        mah_recursive_function(some_arg,
                               some_option=some_option
        )

显然,那些some_option=some_option分配实际上是多余的,并且,由于我有多个永久kwarg选项,冗余分配变得非常烦人。

from functools.magical_stuff import get_current_kwargs
mah_recursive_function(
    some_arg, **get_current_kwargs()
)

我宁愿不用冗余线路膨胀我的递归调用。最好的方法是什么?

2 个答案:

答案 0 :(得分:2)

考虑使用包装函数来避免重复:

def recursive_func_wrapper(arg, option=True):
    def recursive_func(arg):
        if option: recursive_func(new_arg)
        else: recursive_func(new_arg2)
    return recursive_func(arg)

您的选项会自动传播(作为局部变量)到内部函数,它可以像往常一样递归,而不会重复option=参数。

答案 1 :(得分:0)

以下是我认为您希望通过递归实现的最小示例 - 通常每次都使用相同的值,但可以灵活地更改其中的一些。这样做你想要的吗?

关键是获取你的args / kwargs的副本,否则你将在每次更改内容时修改每个未完成的调用的args / kwargs。

你可以用装饰器来装扮它,它可以自动提供原件和副本,甚至可以不必打破当前的名字。

def factorial(*args_this_call, **kwargs_this_call):
    print('-----------------------------'
          '\nargs: {}'
          '\nkwargs: {}'.format(args_this_call, kwargs_this_call))
    # get copies so you don't modify the values of other calls that are waiting to complete
    args_next_call = list(args_this_call)
    kwargs_next_call = kwargs_this_call.copy()

    # you can break out the content of args / kwargs for clarity if you are going to use them a lot
    arg1, arg2 = args_this_call  # not used - just for example
    n = kwargs_this_call['n']

    # do your work
    if n == 1:
        return 1
    else:
        # modify whichever args/kwargs you want, leave the ones you don't want to change
        args_next_call[1] += 1  # some random modification
        kwargs_next_call['call_id'] += 1
        kwargs_next_call['n'] -= 1  # n-1 for factorial in this case

        # make the call
        result = n * factorial(*args_next_call, **kwargs_next_call)
        return result

# test the factorial with args/kwargs
args = (1, 1)  # just some values
kwargs = {'call_id': 1,
          'n': 4}
print(factorial(*args, **kwargs))

结果:

-----------------------------
args: (1, 1)
kwargs: {'call_id': 1, 'n': 4}
-----------------------------
args: (1, 2)
kwargs: {'call_id': 2, 'n': 3}
-----------------------------
args: (1, 3)
kwargs: {'call_id': 3, 'n': 2}
-----------------------------
args: (1, 4)
kwargs: {'call_id': 4, 'n': 1}
24