Executing functions within switch dictionary

前端 未结 3 1461
不知归路
不知归路 2020-12-18 18:58

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 T         


        
3条回答
  •  借酒劲吻你
    2020-12-18 19:28

    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")()
    

提交回复
热议问题