Using a dictionary to select function to execute

前端 未结 10 1988
走了就别回头了
走了就别回头了 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:48

    This will call methods from dictionary

    This is python switch statement with function calling

    Create few modules as per the your requirement. If want to pass arguments then pass.

    Create a dictionary, which will call these modules as per requirement.

        def function_1(arg):
            print("In function_1")
    
        def function_2(arg):
            print("In function_2")
    
        def function_3(fileName):
            print("In function_3")
            f_title,f_course1,f_course2 = fileName.split('_')
            return(f_title,f_course1,f_course2)
    
    
        def createDictionary():
    
            dict = {
    
                1 : function_1,
                2 : function_2,
                3 : function_3,
    
            }    
            return dict
    
        dictionary = createDictionary()
        dictionary[3](Argument)#pass any key value to call the method
    
    0 讨论(0)
  • 2020-11-27 13:49

    Not proud of it, but:

    def myMain(key):
        def ExecP1():
            pass
        def ExecP2():
            pass
        def ExecP3():
            pass
        def ExecPn():
            pass 
        locals()['Exec' + key]()
    

    I do however recommend that you put those in a module/class whatever, this is truly horrible.

    0 讨论(0)
  • 2020-11-27 13:52

    Simplify, simplify, simplify:

    def p1(args):
        whatever
    
    def p2(more args):
        whatever
    
    myDict = {
        "P1": p1,
        "P2": p2,
        ...
        "Pn": pn
    }
    
    def myMain(name):
        myDict[name]()
    

    That's all you need.


    You might consider the use of dict.get with a callable default if name refers to an invalid function—

    def myMain(name):
        myDict.get(name, lambda: 'Invalid')()
    

    (Picked this neat trick up from Martijn Pieters)

    0 讨论(0)
  • 2020-11-27 13:52
    # index dictionary by list of key names
    
    def fn1():
        print "One"
    
    def fn2():
        print "Two"
    
    def fn3():
        print "Three"
    
    fndict = {"A": fn1, "B": fn2, "C": fn3}
    
    keynames = ["A", "B", "C"]
    
    fndict[keynames[1]]()
    
    # keynames[1] = "B", so output of this code is
    
    # Two
    
    0 讨论(0)
  • 2020-11-27 13:56

    Often classes are used to enclose methods and following is the extension for answers above with default method in case the method is not found.

    class P:
    
         def p1(self):
             print('Start')
    
         def p2(self):
             print('Help')
    
         def ps(self):
             print('Settings')
    
         def d(self):
             print('Default function')
    
         myDict = {
             "start": p1,
             "help": p2,
             "settings": ps
         }
    
         def call_it(self):
             name = 'start'
             f = lambda self, x : self.myDict.get(x, lambda x : self.d())(self)
             f(self, name)
    
    
     p = P()
     p.call_it()
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题