在切换字典中执行功能 [英] Executing functions within switch dictionary

查看:80
本文介绍了在切换字典中执行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将我开发的所有模块放入主程序时遇到问题。我创建的switch字典可以在下面看到:

  def Tank_Shape_Calcs(Tank_Shape,level,area,dish,radius,length ,Strapping_Table,Tank_Number):

switcher = {
0:vertical.Vertical_Tank(level,area),
1:horiz.Horiz_Cylinder_Dished_Ends(盘,半径,等级,长度) ,
2:strapping.Calc_Strapped_Volume(Strapping_Table,level),
3:poly.Fifth_Poly_Calcs(Tank_Number)
}
return switcher.get(Tank_Shape,ERROR:Tank type not有效的)

坦克形状在每个坦克的循环中设置在主文件中。第一个坦克有 Tank_Shape = 2 ,所以我期望它执行 Calc_Strapped_Volume()函数。



我尝试过测试它,并且切换器函数肯定读取 Tank_Shape 2 。另外,如果我将函数更改为字符串,它将打印出正确的字符串。

问题在于函数似乎是按顺序执行的,直到调用正确的函数为止。这会导致错误,因为我使用的数据只能使用正确的函数。



有没有办法只执行正确的函数?

$

解决方案

所有函数在构建字典时执行,而不是在您访问密钥时执行。

您需要使用 lambda (没有任何参数,它们已经是已知的)来确保函数仅在需要时被调用:

  switcher = {
0:lambda:vertical.Vertical_Tank(level,area),
1:lambda:horiz.Horiz_Cylinder_Dished_Ends(dish,半径,等级,长度),
2:lambda:strapping.Calc_Strapped_Volume(Strapping_Table,level),
3:lambda:poly.Fifth_Poly_Calcs(Tank_Number)
}

然后在返回时调用,并将错误消息作为返回该lambda的lambda表示:

  return switcher.get(Tank_Shape,lambda:ERROR:Tank type not valid)()


I have encountered a problem when putting all the modules I've developed into the main program. The switch dictionary I've created can be seen below:

def Tank_Shape_Calcs(Tank_Shape, level, area, dish, radius, length, Strapping_Table, Tank_Number):

    switcher = {
        0: vertical.Vertical_Tank(level, area),
        1: horiz.Horiz_Cylinder_Dished_Ends(dish, radius, level, length),
        2: strapping.Calc_Strapped_Volume(Strapping_Table, level),
        3: poly.Fifth_Poly_Calcs(Tank_Number)
    }
    return switcher.get(Tank_Shape, "ERROR: Tank type not valid")

The tank shape is set in the main file in a loop for each of the tanks. The first tank has Tank_Shape = 2 so I would expect it to execute the Calc_Strapped_Volume() function.

I have tried testing it, and the switcher function is definitely reading Tank_Shape as 2. Also if I change the functions to strings, it will print out the correct string.

The problem is that the functions seem to be executed sequentially, until the correct function is called. This results in errors as the data I'm using will only work with the correct function.

Is there a way to only execute the correct function?

解决方案

All your functions are executed when you build the dictionary, not when you access the key.

You need to use lambda (without any parameters, they are already known) to make sure the function is only called when required:

switcher = {
    0: lambda : vertical.Vertical_Tank(level, area),
    1: lambda : horiz.Horiz_Cylinder_Dished_Ends(dish, radius, level, length),
    2: lambda : strapping.Calc_Strapped_Volume(Strapping_Table, level),
    3: lambda : poly.Fifth_Poly_Calcs(Tank_Number)
}

then call when you return, with the error message as a lambda that returns it:

return switcher.get(Tank_Shape, lambda : "ERROR: Tank type not valid")()

这篇关于在切换字典中执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆