Python: passing flags to functions

前端 未结 6 2121
清酒与你
清酒与你 2021-02-09 12:27

For a long time i have been trying to figure out what is the best way to pass flags to python functions. The most straightforward way is something like:

def func         


        
6条回答
  •  佛祖请我去吃肉
    2021-02-09 12:54

    I prefer functions without flags. Rather do this:

    def func_flag1(arg):
        pass # something useful
    
    def func_flag2(arg):
        pass # something for flag 2.
    

    But "flagX" would actually be something meaningful like "do_X_with_option".

    I prefer this because it makes it clearer, you keep the functions simpler (fewer bugs), and you don't have to carry some constants into other modules (in the case of flags actually being some kind of enumeration).

提交回复
热议问题