Python 如何使用用户输入从类中调用我的方法?

Python 如何使用用户输入从类中调用我的方法?,python,python-3.x,Python,Python 3.x,我试图通过请求用户的输入从类中调用我的方法,并使用该输入调用该方法。我认为我的最佳尝试如下所示: class Possibilities: def change_password(self): do_change_password() def do_change_password(): <some code here> call = input('what do you want to do?') Possibilities.call() #her

我试图通过请求用户的输入从类中调用我的方法,并使用该输入调用该方法。我认为我的最佳尝试如下所示:

class Possibilities:
    def change_password(self):
        do_change_password()

def do_change_password():
    <some code here>

call = input('what do you want to do?')
Possibilities.call() #here the error happens
类的可能性:
def更改_密码(自我):
是否更改密码()
def do_change_password():
call=input('您想做什么?')
可能性。调用()#这里发生错误

这有什么解决方案吗?

< P>如果你真的想遵循这个方法,你应该考虑使用<代码>类方法< /代码>。而
call
是一个变量,您应该将它作为参数传递给
方法

例如:

def do_change_password(): 
    <some code here>

class Possibilitiees:
     @classmethod
     def choose_action(cls, action):
         if action == "change password":
             do_change_password()

call = input("what do you want to do?")
Possibilities.choose_action(call)

可能的重复您如何检测这些重复?我根本没找到它!:)谷歌“python名称调用方法”哇!这就是我下次要尝试的这就是我的想法,但我会有多个动作,我不想在同一个方法中产生10个“如果”。例如,你可以用dict键映射函数,而不用考虑类。刚刚编辑了答案
def change_password():
    <code>
def login():
    <code>
def logout():
    <code>

action_mapper = {
    "change": change_password,
    "login": login,
    "logout": logout,
}

call = input("Type an action: ")
# to call your function:
action_mapper[call]()