Python 如何将函数放入字典中以便可以对其进行迭代?

Python 如何将函数放入字典中以便可以对其进行迭代?,python,dictionary,Python,Dictionary,我正在尝试制作一个包含函数作为值的字典。其思想是,难度变量将在整个程序中发生变化,这将影响我们从字典中调用的函数。 出于这个原因,我不打算展示这个程序,我制作了一个简单的模型来演示我遇到的问题 每当我运行代码时,甚至不输入键,它都会运行函数。为什么会这样 def printhello(): print("hello") def printgoaway(): print("go away") x={1:printhello(),2:printgoaway()} 在这一点上,

我正在尝试制作一个包含函数作为值的字典。其思想是,难度变量将在整个程序中发生变化,这将影响我们从字典中调用的函数。 出于这个原因,我不打算展示这个程序,我制作了一个简单的模型来演示我遇到的问题

每当我运行代码时,甚至不输入键,它都会运行函数。为什么会这样

def printhello():
    print("hello")

def printgoaway():
    print("go away")


x={1:printhello(),2:printgoaway()}

在这一点上,我没想到会发生什么,因为我还没有调用任何键。它运行函数并打印值

如果我随后通过执行
x[1]
x[2]
调用它们,则不会发生任何事情


有人能告诉我如何使用字典中的函数,并阻止它们在我创建字典时自动调用它们吗?问题是,你在字典中的函数名后面加了括号:

x={1:printhello(),2:printgoaway()}
这将执行函数并将返回的值放入字典中。请尝试删除括号:

x={1:printhello ,2:printgoaway}
这将函数本身放入字典中

例如,以下是我的一个程序的一部分:

OPERATIONS = {'+': sum, '*': rdmath.prod}

def evaluate_subtree(node_list, ndx: int) -> int:
    """Evaluate the subtree inside node_list with the root at ndx.
    Use recursion and postorder in depth first search."""
    this_payload, these_children = node_list[ndx]
    if not these_children:  # if a leaf node
        return int(this_payload)
    return OPERATIONS[this_payload](evaluate_subtree(node_list, child)
                                    for child in these_children)
请注意,我将
sum
函数和
rdmath.prod
(它计算iterable成员的乘积)放入字典
OPERATIONS
。我的代码片段的最后一行使用字典选择两个函数中的一个,然后在生成器上执行该函数,并返回结果值。(因此,将生成器中的值相加或相乘。)


尝试类似的方法,看看它是否适合您。

问题是您在字典中的函数名后面放了括号:

x={1:printhello(),2:printgoaway()}
这将执行函数并将返回的值放入字典中。请尝试删除括号:

x={1:printhello ,2:printgoaway}
这将函数本身放入字典中

例如,以下是我的一个程序的一部分:

OPERATIONS = {'+': sum, '*': rdmath.prod}

def evaluate_subtree(node_list, ndx: int) -> int:
    """Evaluate the subtree inside node_list with the root at ndx.
    Use recursion and postorder in depth first search."""
    this_payload, these_children = node_list[ndx]
    if not these_children:  # if a leaf node
        return int(this_payload)
    return OPERATIONS[this_payload](evaluate_subtree(node_list, child)
                                    for child in these_children)
请注意,我将
sum
函数和
rdmath.prod
(它计算iterable成员的乘积)放入字典
OPERATIONS
。我的代码片段的最后一行使用字典选择两个函数中的一个,然后在生成器上执行该函数,并返回结果值。(因此,将生成器中的值相加或相乘。)

尝试类似的方法,看看是否适合您。

printhello()运行printhello

使用
x={1:printhello,2:printgoaway}
存储函数,并使用
x[1]()
调用它

printhello()运行printhello

使用
x={1:printhello,2:printgoaway}
存储函数,在函数名后放置
()
时,使用
x[1]()
调用函数,这会告诉Python立即调用函数

您应该更改代码以执行此操作:

x={1:printhello,2:printgoaway}
因此,您的字典包含对函数的引用,而不是预先调用函数的结果

然后你可以这样给他们打电话:

x[0]()
请注意这里的括号,以及前一行中缺少括号。

当您在函数名后放置
()
时,这会告诉Python立即调用函数

您应该更改代码以执行此操作:

x={1:printhello,2:printgoaway}
因此,您的字典包含对函数的引用,而不是预先调用函数的结果

然后你可以这样给他们打电话:

x[0]()

请注意此处的括号,以及前一行中缺少括号。

“在这一点上,我并不期望发生任何事情,因为我还没有调用任何键。”没有人调用键。一个调用函数,您在这里执行了:
printhello()
和这里:
printgoaway()
。换句话说,字典中的值不是函数对象,它们是调用函数对象的结果,这些函数根本不是函数。但在这种情况下,请确保验证它们是否是您想要的函数,通常需要一个
cmd\uu
前缀或类似的前缀。“在这一点上,我并不期望发生任何事情,因为我还没有调用任何键。”人们不会调用键。一个调用函数,您在这里执行了:
printhello()
和这里:
printgoaway()
。换句话说,字典中的值不是函数对象,它们是调用函数对象的结果,这些函数根本不是函数。但在这种情况下,请确保验证它们是否是您想要的函数,通常需要
cmd\uu
前缀或类似前缀。