简体   繁体   中英

Python functions recursively calling each other using dictionary storing their name

I have following scenario:

  • there are multiple function each accepting certain arguments
  • they call each other based on arguments recursively/iteratively till certain conditions that can be inferred from arguments are met
  • I can do if-elif in those functions, but since that will cause a lot of if-elif inside all of these functions, I thought I should use dictionary storing reference to these functions against their name as a key and then hash into this dictionary (using argument contents) to obtain and call the function to be called.

The issue is that I am not able to decide where to define that dictionary, before all functions (as all functions will be using this dictionary) or after all functions (as dictionary will use all these functions).

Below I tried to imitate the scenario. I used random function do decide upon which function to call instead of inferring it from the arguments. Also I have used recurCount to decide when to stop recursive calls.

import random

# funcDict = {"fun1": fun1,
#             "fun2": fun2,
#             "fun3": fun3,
#             "fun4": fun4}

#Traceback (most recent call last):
#  File "C:\...\temp.py", line 107, in <module>
#    funcDict = {"fun1": fun1,
#NameError: name 'fun1' is not defined

funcList = ["fun1","fun2","fun3","fun4"]
recurCount = 5

def fun1():
    global recurCount
    print("fun1")
    if(recurCount == 0):
        return 
    else:
        recurCount= recurCount-1
        funcDict[random.choice(funcList)]()  #recursive call

def fun2():
    global recurCount
    print("fun2")
    if(recurCount == 0):
        return 
    else:
        recurCount= recurCount-1
        funcDict[random.choice(funcList)]()  #recursive call

def fun3():
    global recurCount
    print("fun3")
    if(recurCount == 0):
        return 
    else:
        recurCount= recurCount-1
        funcDict[random.choice(funcList)]()  #recursive call

def fun4():
    global recurCount
    print("fun4")
    if(recurCount == 0):
        return 
    else:
        recurCount= recurCount-1
        funcDict[random.choice(funcList)]()  #recursive call

fun1()

# funcDict = {"fun1": fun1,
#             "fun2": fun2,
#             "fun3": fun3,
#             "fun4": fun4}

#Traceback (most recent call last):
#  File "C:\...\temp.py", line 152, in <module>
#    fun1()
#  File "C:\...\temp.py", line 123, in fun1
#    funcDict[random.choice(funcList)]()
#NameError: name 'funcDict' is not defined

The dictionary requires that the functions are already defined, while the first call to any of the functions requires that the dictionary is already defined. Therefore, you should define the dictionary after all the function definitions and before making the first call to any of the functions:

def fun1():
    ...

def fun2():
    ...

def fun3():
    ...

def fun4():
    ...

funcDict = {"fun1": fun1,
            "fun2": fun2,
            "fun3": fun3,
            "fun4": fun4}

fun1()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM