您能阻止函数在python中的字典映射内执行吗?

您能阻止函数在python中的字典映射内执行吗?,python,function,dictionary,Python,Function,Dictionary,将函数映射到字典时,python会在调用键之前自动执行该函数。这能预防吗? 例如: def testfunc(): print 'Executed' testdict = { '1': testfunc(),} >>>EXECUTED 如果没有,有没有更好的方法?这是对decorators的调用还是什么?简单地说,映射时请删除函数名称后的括号: testdict = {'1': testfunc,} # remove the parentheses, in orde

将函数映射到字典时,python会在调用键之前自动执行该函数。这能预防吗? 例如:

def testfunc():
    print 'Executed'

testdict = { '1': testfunc(),}
>>>EXECUTED

如果没有,有没有更好的方法?这是对decorators的调用还是什么?

简单地说,映射时请删除函数名称后的括号:

testdict = {'1': testfunc,} # remove the parentheses, in order to save the function, 
                            # not its return value
&称之为:

testdict['1']()

简单地说,映射时删除函数名称后的括号:

testdict = {'1': testfunc,} # remove the parentheses, in order to save the function, 
                            # not its return value
&称之为:

testdict['1']()

创建字典的方式将
testfunc()
的返回值映射到键
'1'
。为了知道返回值,必须首先执行函数。如果您只是想在字典中保存函数,请执行以下操作:

testdict = {'1': testfunc,}

创建字典的方式将
testfunc()
的返回值映射到键
'1'
。为了知道返回值,必须首先执行函数。如果您只是想在字典中保存函数,请执行以下操作:

testdict = {'1': testfunc,}

是的,您可以通过不调用方法来阻止执行pf方法,也可以通过不提供括号来阻止menas方法的执行。 无论何时,只要您想调用该方法,请参见以下内容:

def testfunc():
    print 'Executed'
testdict = {'1': testfunc,}
testdict['1']()
Executed

是的,您可以通过不调用方法来阻止执行pf方法,也可以通过不提供括号来阻止menas方法的执行。 无论何时,只要您想调用该方法,请参见以下内容:

def testfunc():
    print 'Executed'
testdict = {'1': testfunc,}
testdict['1']()
Executed

省略函数调用操作符,
()
,so
'1':testfunc
省略函数调用操作符,
()
,so
'1':testfunc
如果要将参数传递给函数,您将如何处理?