如何调用不是字符串的函数?

时间:2019-05-30 00:45:33

标签: python python-3.x

如何以完全相同的方式在另一个函数内部加载一些代码的函数?

如何将var arr = ['apples',null,'strawberries'] let index = arr.findIndex(x => !x) console.log(index) console.log(arr[index])正确地加载到我的global_variables()step1()函数中,而不必在每个函数中复制/粘贴它?

step2()

我希望:

    def global_variables():
        global txt   
        global option_a
        global option_b
        global option_c
        global next_function_a
        global next_function_b
        global next_function_c

    def step1():
        global_variables()

        txt = step1_text # fill with correct text

        option_a = step1a_text
        option_b = step1b_text
        option_c = step1c_text

        next_function_a = step1_1 # variable = call function
        next_function_b = step2 # variable = call function
        next_function_c = step5 # variable = call function

        start_step() # call step function

    def step2():
        global_variables()

        txt = step2_text # fill with correct text

        option_a = step2a_text
        option_b = step2b_text
        option_c = step2c_text

        next_function_a = game_over(step2c_text) # variable = call function
        next_function_b = step3 # variable = call function
        next_function_c = step2_1 # variable = call function

        start_step() # call step function

被包含在 global txt global option_a global option_b global option_c global next_function_a global next_function_b global next_function_c step1()函数中,就像每次手动在其中手动复制/粘贴一样。

step2()

2 个答案:

答案 0 :(得分:1)

正如评论所建议的那样,您可能必须手动对其进行全局设置,但是有很多解决方法

您可以执行以下操作:

def step(numb):
    global txt   
    global option_a
    global option_b
    global option_c
    global next_function_a
    global next_function_b
    global next_function_c
    if num == 1:
        txt = step1_text # fill with correct text

        option_a = step1a_text
        option_b = step1b_text
        option_c = step1c_text

        next_function_a = step1_1 # variable = call function
        next_function_b = step2 # variable = call function
        next_function_c = step5 # variable = call function

        start_step() # call step function
    elif numb == 2:
        txt = step2_text # fill with correct text

        option_a = step2a_text
        option_b = step2b_text
        option_c = step2c_text

        next_function_a = game_over(step2c_text) # variable = call function
        next_function_b = step3 # variable = call function
        next_function_c = step2_1 # variable = call function

这样,您可以编写更多的步骤

希望它会有所帮助:)

答案 1 :(得分:0)

如果这些变量名存在于定义函数的模块范围内,那么您将需要在step1() step2()中为代码进行全局声明去工作。

相反,您可以在模块范围内创建字典,然后在step1()step2()中修改该字典

gv_s = {'txt':None,
        'option_a':None,
        'option_b':None,
        'option_c':None,
        'next_function_a':None,
        'next_function_b':None,
        'next_function_c':None}

def step1():

    gv_s['txt'] = step1_text # fill with correct text

    gv_s['option_a'] = step1a_text
    gv_s['option_b'] = step1b_text
    gv_s['option_c'] = step1c_text

    gv_s['next_function_a'] = step1_1 # variable = call function
    gv_s['next_function_b'] = step2 # variable = call function
    gv_s['next_function_c'] = step5 # variable = call function

    start_step() # call step function

def step2():

    gv_s['txt'] = step2_text # fill with correct text

    gv_s['option_a'] = step2a_text
    gv_s['option_b'] = step2b_text
    gv_s['option_c'] = step2c_text

    gv_s['next_function_a'] = game_over(step2c_text) # variable = call function
    gv_s['next_function_b'] = step3 # variable = call function
    gv_s['next_function_c'] = step2_1 # variable = call function

    start_step() # call step function

step_end重构为:

def step_end(choice):
    if choice =="A":
        print(gv_s['option_a'])
        gv_s['next_function_a']()
    elif choice =="B":
        print(gv_s['option_b'])
        gv_s['next_function_b']()
    elif choice =="C":
        print(gv_s['option_c'])
        gv_s['next_function_c']()

那将使它起作用,但仍然可能不是组织过程的最佳方法。


step1step2的另一个选项可能只是返回选项字典并将返回值分配给模块命名空间中的名称。

def step1():
    return {'txt':step1_text,
            'option_a':step1a_text,
            'option_b':step1b_text,
            'option_c':step1c_text,
            'next_function_a':step1_1,
            'next_function_b':step2,
            'next_function_c':step5}

def step2():
    return {'txt':step2_text,
            'option_a':step2a_text,
            'option_b':step2b_text,
            'option_c':step2c_text,
            'next_function_a':game_over(step2c_text),
            'next_function_b':step3,
            'next_function_c':step2_1}

gv_s = step1()
# or
#gv_s = step2()
start_step()

通过step_end访问模块级字典

def step_end(choice):
    if choice =="A":
        print(gv_s['option_a'])
        gv_s['next_function_a']()
    elif choice =="B":
        print(gv_s['option_b'])
        gv_s['next_function_b']()
    elif choice =="C":
        print(gv_s['option_c'])
        gv_s['next_function_c']()

您可以use a class来代替字典。