-1

I am trying to call my method from my class by asking an input from user, and use the input for calling the method. i think my best try looks like this:

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

Is there any solution for this?

Dogg1
  • 151
  • 1
  • 1
  • 13

1 Answers1

0

If you really want following this approach you should consider using a classmethod. and call is a variable that you should pass to a Possibilities method as an argument.

for example:

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)

EDIT

you can also forget about classes and go for mappers as in:

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]()
  • That's what I am thinking, but I'll have multiple actions and I don't want to make like 10 ifs in same method. – Dogg1 Oct 15 '17 at 12:01
  • you could map with a dict keys to functions for example and forget about classes. just edited the answer –  Oct 15 '17 at 12:03