如何在类中实现递归函数的装饰器

Vis*_*udi 0 python recursion decorator

我正在编写一个在初始化时接受整数输入列表的类。该类有很多排序方法。我想添加一个装饰器,它会在每次方法调用之前对输入列表进行洗牌。当尝试实现递归冒泡排序时,装饰器会导致RecursionError: maximum recursion depth exceeded in comparison

我尝试传递 self 参数,以便装饰器可以访问类变量。但是我需要有关如何让递归函数与装饰器一起使用的帮助

import functools
from searching import timer
import random


def shuffle(func):
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        random.shuffle(self.ip_list)
        value = func(self, *args, **kwargs)
        return value
    return wrapper


class sorting:
    def __init__(self, ip_list):
        self.ip_list = ip_list
        self.n = len(self.ip_list)
        self.timer_dict = {}

    @shuffle
    @timer
    def recursive_bubble_sort(self):
        print(self.ip_list)
        for j in range(self.n):
            try:
                if self.ip_list[j] > self.ip_list[j+1]:
                    self.ip_list[j], self.ip_list[j + 1] = self.ip_list[j + 1], self.ip_list[j]
                    self.recursive_bubble_sort()
            except IndexError:
                pass
        print(self.ip_list)


x = [i for i in range(0,30)]
s = sorting(x)
s.recursive_bubble_sort()
Run Code Online (Sandbox Code Playgroud)

Blc*_*ght 5

装饰像您的示例中那样的递归方法是一个非常糟糕的主意。对于某些方法和装饰器来说它可以工作,但不能作为排序算法。问题是每个递归调用最终都会通过装饰器的包装器进行调用。使用shuffle装饰器,这意味着您将在每次递归调用时重新排列列表,这就是为什么您的列表永远不会被排序的原因。即使排序没有在每次调用时重新洗牌,您的timer装饰器也可能会遇到类似的问题,因为它会尝试为每个递归调用计时,而不仅仅是对函数的顶层调用。

一种选择可能是将递归方法和修饰方法分开。这通常是为无论如何都要通过递归实现的函数设计 API 的好方法,因为您经常需要将额外的参数传递到递归调用中,但顶层调用不需要它们。

@shuffle
@timer
def bubble_sort_recursive(self):        # despite the name, this function is not recursive itself
    self.bubble_sort_recursive_helper()

def bubble_sort_recursive_helper(self): # all the recursion happens in this helper method
    ... # recursive code goes here, recursive calls should be to the helper!
Run Code Online (Sandbox Code Playgroud)