Write a class with methods, use a dictionary like a switch statement

I have spent at least a day or more trying to write a class that has several methods and then use a dictionary with the structure {key: method} where the key is a string and when accessed calls the method. When the method completes it returns to the dictionary. So the dictionary acts like a switch statement which is a common use in python. Just can’t get the methods to work when I give the key. I have tried many variations. Is this possible if so how ? I may finish what I’m currently working on before this is answered but I think its still an interesting concept that could have many uses in the future.

hi,

Could you post here a sample code to be able to test and see it in action and know what you mean exactly? Thanks

I no longer have the code deleted it partly out of frustration and I have to keep moving, I have already spent a lot of time in the course I’m currently in than I should have. Basically it’s goes like:

class MyMethods(object):

   def method1(self):
       
  def method2(self):

 def methodx(self):

create a dictionary that works like this:

def select_method(method):
switch = {
“straight” : method1,
“rotate” : method2,
“stop” : method3,
“xxx” : methodx,
“exit” : exit
}

  return switch.get(method, "rotate")

Where “rotate” is the key that implements the method, which when finished returns back to the switch method until you decide to exit so it would exist inside a loop or possibly use spin if using it for ROS. I was also thinking about it in more general terms so it could be used anytime you want or needed something like this. I simply didn’t have any more time to put into it. I spent a little more than a day researching and playing with different variations none of which worked. So I posted to see if their was something out their or if somebody with a deeper understanding of python might know how to get this working. If theirs no response I do plan on taking this up again at some point unless someone provides a solution or says it’s not possible. I hope it is it’s an interesting problem. In hind sigh I could use this to solve all the problems and quizzes I have done so far some in a much more elegant fashion. I’m always seeking the simplest solution that accomplishes a task whatever it may be. I have also posted on stackexchange.com.