Play an array of C function pointers in Python

I have a Python program asking the user to enter as a shell, and if I discover some specific keywords, I want to go into some specific functions.

The thing is, I would like to avoid a lot of if and else if . Usually in C, to avoid this situation, I use an array of function pointers, which I move with while , and use strcmp to validate the input.

I would like to know how to do this in Python, if possible.

+6
source share
1 answer

In Python, you use a dictionary .

Example:

 keyword2func = { "word1": function_word1, "word2": function_word2, } word = input("") keyword2func[word]() 
+7
source

Source: https://habr.com/ru/post/987162/


All Articles