Pythonic way to have a choice of 2-3 options as an argument to a function

前端 未结 6 789
抹茶落季
抹茶落季 2021-02-07 07:16

I have a Python function which requires a number of parameters, one of which is the type of simulation to perform. For example, the options could be \"solar\", \"view\" or \"bot

6条回答
  •  无人共我
    2021-02-07 07:47

    You can use the assert statement like this:

    assert sim_types in ['solar', 'view', 'both'], 'sim type parameter must be solar, view or both'
    

    If sim_types is not in the list, python will raise an Assertion Error

提交回复
热议问题