Skip to content
Advertisement

Executing functions through dictionaries

My script executes depending on user’s input, like this:

JavaScript

The problem is that I can have over 15 different choices, so I would like to make it cleaner using dicts, but I can’t get it to work. This is what I did. In the class init for sh:

JavaScript

And so on. Further down in the class:

JavaScript

And finally the input part of my script is much cleaner

JavaScript

This is as far as I got, and while it works for options without parameters, how can I make it work also for commands that need a second parameter?

Advertisement

Answer

To create a dispatcher, you need to combine the function/dictionary/lambdas like this:

JavaScript

which you can call with self.operations("s1", 2). To evaluate it and get the result, you may need to do self.operations("s1", 2)().

Or, you need a value evaluated and returned, you can also write the function with the () at the end:

JavaScript

lambda: None, is the default for what happens if no valid key is passed. It needs to be a lambda to avoid getting: TypeError: 'NoneType' object is not callable (because None itself isn’t), but you can change this to something that makes sense for your purpose.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement