How should I implement the following class? I want to create class that executes methods in random order when called and after all methods are called once reset array and reshuffle?
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()
These are the two ideas I came up with, but I'm still wondering what would be the smartest way to implement this sort of class?
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
reset()method. Factor it out. Don't forget to refill the depleted list. Modulecopycould help you copy a list, or you could use thecopy_of_list = source_list[:]idiom.