Python 如何取消字典初始化?蟒蛇3.3
通过首先解释代码,可以更容易地解释我的问题Python 如何取消字典初始化?蟒蛇3.3,python,dictionary,python-3.x,Python,Dictionary,Python 3.x,通过首先解释代码,可以更容易地解释我的问题 def initialize_function(num,instruction,emplacement1,emplacement2,current_pipeline): function_mapping={ "LOAD" : LOAD(num,emplacement1,emplacement2,current_pipeline), "STORE" : STORE(num,emplacement1,emplacement2,cur
def initialize_function(num,instruction,emplacement1,emplacement2,current_pipeline):
function_mapping={
"LOAD" : LOAD(num,emplacement1,emplacement2,current_pipeline),
"STORE" : STORE(num,emplacement1,emplacement2,current_pipeline),
"MOVE" : MOVE_IADD(num,emplacement1,emplacement2,current_pipeline),
"IADD" : MOVE_IADD(num,emplacement1,emplacement2,current_pipeline),
"FADD" : FADD(num,emplacement1,emplacement2,current_pipeline)
}
current_pipeline=function_mapping[instruction]
return(current_pipeline)
initialize_函数
函数有一个参数指令
<代码>指令是一个字符串,相当于函数映射
字典的一个键。
因此,当我执行current\u pipeline=function\u mapping[instruction]
时,应该只执行指令的值。但事实并非如此。实际上,函数映射
字典在查找键指令
之前已初始化,因此它会一个接一个地执行所有函数LOAD、STORE、MOVE、IADD、FADD
我能做什么
提前感谢:)
MFF由于所有函数的参数都相同,因此应该可以:
def initialize_function(num,instruction,emplacement1,emplacement2,current_pipeline):
function_mapping={
"LOAD" : LOAD,
"STORE" : STORE,
"MOVE" : MOVE_IADD,
"IADD" : MOVE_IADD,
"FADD" : FADD
}
current_pipeline=function_mapping[instruction](num,emplacement1,emplacement2,current_pipeline)
return(current_pipeline)
说明:由于您实际上正在调用函数,因此字典值将在运行时进行计算。您希望传递对它们的引用,因为函数是python中的第一类对象,所以您可以这样做。因为所有函数的参数都是相同的,所以应该可以:
def initialize_function(num,instruction,emplacement1,emplacement2,current_pipeline):
function_mapping={
"LOAD" : LOAD,
"STORE" : STORE,
"MOVE" : MOVE_IADD,
"IADD" : MOVE_IADD,
"FADD" : FADD
}
current_pipeline=function_mapping[instruction](num,emplacement1,emplacement2,current_pipeline)
return(current_pipeline)
说明:由于您实际上正在调用函数,因此字典值将在运行时进行计算。您希望传递对它们的引用,因为函数是python中的第一类对象,所以您可以这样做