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

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

    I don't like any of those options.

    I'd define two different functions, perform_solar(a, b, c) and perform_view(a, b, c) and let the caller decide which ones he wants to use, in which order and with which arguments.

    If the reason why you thought you'd have to pack these into one single function is that they share state, you should share that state in an object and define the functions as methods.

提交回复
热议问题