简体   繁体   English

Python函数使用存储其名称的字典递归调用彼此

[英]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. 我可以在这些函数中执行if-elif ,但是由于这会在所有这些函数中引起大量的if-elif ,因此我认为我应该使用字典将对这些函数的引用作为它们的名称存储为键,然后将其哈希到该字典中(使用参数内容)获取并调用要调用的函数。

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. 我也使用recurCount来决定何时停止递归调用。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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