Using a dictionary to select function to execute

前端 未结 10 2086
走了就别回头了
走了就别回头了 2020-11-27 13:05

I am trying to use functional programming to create a dictionary containing a key and a function to execute:

myDict={}
myItems=(\"P1\",\"P2\",\"P3\",....\"         


        
10条回答
  •  春和景丽
    2020-11-27 13:57

    #!/usr/bin/python
    
    def thing_a(arg=None):
        print 'thing_a', arg
    
    def thing_b(arg=None):
        print 'thing_b', arg
    
    ghetto_switch_statement = {
        'do_thing_a': thing_a,
        'do_thing_b': thing_b
    }
    
    ghetto_switch_statement['do_thing_a']("It's lovely being an A")
    ghetto_switch_statement['do_thing_b']("Being a B isn't too shabby either")
    
    print "Available methods are: ", ghetto_switch_statement.keys()
    

提交回复
热议问题