better writing style for the following code

后端 未结 5 646
借酒劲吻你
借酒劲吻你 2021-01-28 09:42

I was wondering if someone could tell me the pythonic way to check out the following.

I have a 6 bit binary number and want to check with its decimal values. Using mathe

5条回答
  •  一向
    一向 (楼主)
    2021-01-28 09:56

    I would use a decorator to map into a dictionary based dispatch:

    _dispatch_table = {}
    
    def dispatch_on(*values):
        def dec(f):
            _dispatch_table.update((v, f) for v in values)
            return f
        return dec
    
    @dispatch_on(0, 2, 47)
    def one():
        foo()
        bar()
    
    @dispatch_on(2, 23, 89)
    def two():
        bar()
        baz()
    
    x = some_number
    _dispatch_table[x]()    
    

提交回复
热议问题