Python dynamic function names

前端 未结 8 1518
星月不相逢
星月不相逢 2020-12-29 13:54

I\'m looking for a better way to call functions based on a variable in Python vs using if/else statements like below. Each status code has a corresponding function



        
8条回答
  •  盖世英雄少女心
    2020-12-29 14:03

    Some improvement to SilentGhost's answer:

    globals()[status.lower()](*args, **kwargs)
    

    if you want to call the function defined in the current module.

    Though it looks ugly. I'd use the solution with dictionary.

提交回复
热议问题