我应该如何实现以下课程?我想创建在调用时以随机顺序执行方法的类,并且在所有方法都被调用一次重置数组和重新洗牌之后?
import random
class RandomFunctions(object):
def f1():
print("1")
def f2():
print("2")
def f3():
print("3")
f = [f1, f2, f3]
def __init__(self):
super(RandomFunctions, self).__init__()
random.shuffle(self.f)
def execute(self):
func = self.f.pop()
if not self.f:
reset f
return func
def main():
f = RandomFunctions()
for i in range(6):
f.execute()()
main()
这是我提出的两个想法,但我仍然想知道实现这种类的最聪明的方法是什么?
discard = []
n = 0
def execute(self):
func = self.f[self.n]
self.n += 1
if self.n == len(self.f):
self.n = 0
random.shuffle(self.f)
return func
def execute_with_discard(self):
func = self.f.pop(0)
discard.append(func)
if not self.f:
f = discard[:]
discard = []
random.shuffle(self.f)
return func