Python: passing flags to functions

前端 未结 6 2111
清酒与你
清酒与你 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 13:11

    I am missing the good old bitwise flags:

    a = 1
    b = 2
    c = 4
    d = 8
    
    def func( data, flags ):
        print( ( flags & a) == a )
        print( ( flags & b) == b )
        print( ( flags & c) == c )
        print( ( flags & d) == d )
    
    >>>> func("bla", a|c|d)
    >>>> True
    >>>> False
    >>>> True
    >>>> True
    

提交回复
热议问题