Using a dictionary to select function to execute

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

    You are wasting your time:

    1. You are about to write a lot of useless code and introduce new bugs.
    2. To execute the function, your user will need to know the P1 name anyway.
    3. Etc., etc., etc.

    Just put all your functions in the .py file:

    # my_module.py
    
    def f1():
        pass
    
    def f2():
        pass
    
    def f3():
        pass
    

    And use them like this:

    import my_module
    
    my_module.f1()
    my_module.f2()
    my_module.f3()
    

    or:

    from my_module import f1
    from my_module import f2
    from my_module import f3
    
    f1()
    f2()
    f3()
    

    This should be enough for starters.

提交回复
热议问题