mercredi 13 janvier 2021

Changing the order of called functions randomly

I have a function whose outline is given below. In main(), I want to return the returned values of one of the functions but I want to choose it randomly. As of now, it checks func1 first and proceeds only if func1 is some_val. I want to be able to check func2 first sometimes as well.

I realize that I could call both functions, create a list with the outcomes, and randomly shuffle the list but both func1 and func2 are quite a bit involved, so performance is an issue.

Is there a clean way to do it?

def func1():
    
    ... do things
    
    return val

def func2():
    
    ... do things
    
    return val



def main():
    
    if func1() is not some_val:
        return func1()
    
    elif func2() is not some_val:
        return func2()
    
    else:
        return None



Aucun commentaire:

Enregistrer un commentaire