我正在编写一个在初始化时接受整数输入列表的类。该类有很多排序方法。我想添加一个装饰器,它会在每次方法调用之前打乱输入列表。尝试实现递归气泡排序时,装饰器会导致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()
装饰像示例中那样的递归方法是一个非常糟糕的主意。对于某些方法和装饰器,它可以工作,但不能使用排序算法。问题是每个递归调用最终都会通过装饰器的包装器调用。使用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!