Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)
closed by

I'm actually trying to utilize the functional program to make a dictionary which contains a key and function to run:

myDict={}

myItems=("P1","P2","P3",...."Pn")

def myMain(key):

    def ExecP1():

        pass

    def ExecP2():

        pass

    def ExecP3():

        pass

        ...

    def ExecPn():

        pass  

I need to do something like the below code:

    for myitem in myItems:

        myDict[myitem] = ??? #to dynamically find the corresponding function

So my inquiry is, How would I make a list of all the Exec functions and afterward relegate them to the ideal thing utilizing the dictionary? so toward the end I will have myDict["P1"]() #this will call ExecP1() 

My genuine issue is that I have huge loads of those things and I making a library that will deal with them so the last client just necessities to call myMain("P1")

My reason to avoid:

def ExecPn():

    pass

myDict["Pn"]=ExecPn

is that I need to secure code as I am utilizing it to give a scripting highlight inside my application. 

closed

4 Answers

0 votes
by (15.4k points)
selected by
 
Best answer
To construct a dictionary that associates keys with corresponding functions and dynamically assign the functions to the dictionary items, you can follow the outlined approach:

myDict = {}

myItems = ("P1", "P2", "P3", ..., "Pn")

def myMain(key):

    def ExecP1():

        # Implementation of ExecP1 function

    def ExecP2():

        # Implementation of ExecP2 function

    def ExecP3():

        # Implementation of ExecP3 function

    # Define the remaining Exec functions

    def ExecPn():

        # Implementation of ExecPn function

    # Create a list of the Exec functions

    exec_functions = [ExecP1, ExecP2, ExecP3, ..., ExecPn]

    # Assign the functions to the corresponding items in myDict

    for i, myitem in enumerate(myItems):

        myDict[myitem] = exec_functions[i]

    # Call the desired function using the dictionary

    myDict[key]()

# Example usage

myMain("P1")  # Invokes the ExecP1 function

In this approach, you begin by creating an empty dictionary called myDict and define myItems as a tuple containing the desired keys. Within the myMain function, you proceed to define individual functions for each "ExecP" item, such as ExecP1, ExecP2, and so on, ensuring to implement the necessary functionality within each function.

To dynamically assign these functions to the corresponding items in the myDict dictionary, you create a list called exec_functions that holds references to the Exec functions in the desired order. By iterating over myItems using enumerate(), you assign the appropriate function from exec_functions to each item in myDict.

To invoke a specific function, you can retrieve it from myDict using the desired key and execute it by adding () at the end.

By following this approach, you can effectively create a dictionary that associates keys with corresponding functions, dynamically assigning them based on the items in myItems, and ultimately allowing for the execution of the desired function through the myDict dictionary.
0 votes
by (26.4k points)

Look at the below code:

def myMain(key):

    def ExecP1():

        pass

    def ExecP2():

        pass

    def ExecP3():

        pass

    def ExecPn():

        pass 

    locals()['Exec' + key]()

I do anyway suggest that you put those in a module/class whatever, this is genuinely frightful.

Want to become an expert in Python? Join the python course fast!

For more details, do check out the below video tutorial...

0 votes
by (25.7k points)
To create a dictionary that associates keys with corresponding functions, and dynamically assign the functions to the dictionary items, you can utilize the following approach:

myDict = {}

myItems = ("P1", "P2", "P3", ..., "Pn")

def myMain(key):

    def ExecP1():

        pass

    def ExecP2():

        pass

    def ExecP3():

        pass

    # Define the remaining Exec functions

    def ExecPn():

        pass

    # Create a list of the Exec functions

    exec_functions = [ExecP1, ExecP2, ExecP3, ..., ExecPn]

    # Assign the functions to the corresponding items in myDict

    for i, myitem in enumerate(myItems):

        myDict[myitem] = exec_functions[i]

    # Call the desired function using the dictionary

    myDict[key]()

# Example usage

myMain("P1")  # Calls ExecP1()

In this approach, you create a list called exec_functions that contains all the Exec functions in the desired order. Then, you iterate over myItems using enumerate() to access both the index and the item itself. Inside the loop, you assign the corresponding function from exec_functions to the myDict dictionary using myitem as the key.

Finally, when you want to call a specific function, you can retrieve it from myDict using the desired key and execute it by adding () at the end.

This approach allows you to dynamically assign the functions to the dictionary based on the items in myItems while providing the flexibility to call the desired function using myDict[key]().

Note: Make sure to replace the pass statements in the Exec functions with the actual code logic you want to execute.
0 votes
by (19k points)
To achieve the goal of creating a dictionary that maps keys to corresponding functions, with the ability to dynamically assign functions to dictionary items, follow these steps:

Initialize an empty dictionary called myDict.

Define a tuple named myItems that contains the desired key values, such as ("P1", "P2", "P3", ..., "Pn").

Implement the necessary functions, such as ExecP1, ExecP2, ExecP3, and so on, each associated with the specific functionality required.

Create a list called exec_functions and populate it with references to the corresponding Exec functions in the desired order.

Iterate over myItems using enumerate() to access both the index and the item itself.

Within the loop, assign the corresponding function from exec_functions to the respective key in myDict.

To call a specific function, retrieve it from myDict using the desired key and execute it by appending () to the function name.

By following these steps, you will successfully create a dictionary (myDict) that links keys with their respective functions, dynamically assigning them based on the items in myItems. This approach enables you to easily invoke the desired function by using myDict[key]() notation.

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...