Python functions recursively calling each other using dictionary storing their name
<p>I have following scenario:</p>

<ul>
<li>there are multiple function each accepting certain arguments</li>
<li>they call each other based on arguments recursively/iteratively till certain conditions that can be inferred from arguments are met</li>
<li>I can do <code>if-elif</code> in those functions, but since that will cause a lot of <code>if-elif</code> 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. </li>
</ul>

<p>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).</p>

<p>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 <code>recurCount</code> to decide when to stop recursive calls.</p>

<pre><code>import random

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

#Traceback (most recent call last):
#  File "C:\...\temp.py", line 107, in &lt;module&gt;
#    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 &lt;module&gt;
#    fun1()
#  File "C:\...\temp.py", line 123, in fun1
#    funcDict[random.choice(funcList)]()
#NameError: name 'funcDict' is not defined
</code></pre>

<p>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 <em>after</em> all the function definitions and <em>before</em> making the first call to any of the functions:</p>

<pre><code>def fun1():
    ...

def fun2():
    ...

def fun3():
    ...

def fun4():
    ...

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

fun1()
</code></pre>

python recursion