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

前端 未结 6 799
抹茶落季
抹茶落季 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:33

    You can use optional (keyword) arguments like this

    def func(a, b, c, **kw):
        if kw.get('do_solar'):
            # Do Solar
        if kw.get('do_view'):
            # Do view
    

提交回复
热议问题