Executing functions within switch dictionary

前端 未结 3 1457
不知归路
不知归路 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:23

    What you are doing in your code, is creating a dictionary with integer keys (0-3) and function results as values. Hence, you first call all the functions and then access the return values of these functions. I would change your code as following:

    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,))
        }
        func, args = switcher.get(Tank_Shape, (None, None))
        if func is not None: 
            return func(*args)
    

    Here, you get first the function you want to call with the according arguments and call it.

提交回复
热议问题