I am trying to call a function from a class based on user input. I've tried other examples based on this but keep on getting various errors depending on which direction I try it.
The test code that I am using is
def one():
print('one hahah')
def two():
print('two hahah')
def the_count():
print('I am the count who likes to count')
dispatcher = {
'one': one, 'two': two, 'three': the_count
}
action = input('Option: - ')
jo.dispatcher[action]()
There we have what I want, but once I added the self argument things stopped working properly. Here is my actual code...
import math
class femblem(object):
def j(self):
print('hi')
`data goes here`
...
def __init__(self,lv,name):
self.lv = lv
self.name = name
dispatcher = {
'j': j(self)
}
action = input('Character: - ')
unit1 = femblem(5,"joshua")
unit1.dispatcher[action]()
returns NameError: name 'self' is not defined if I take out self it gives me an error saying that it needs that for an argument (which it does). Not very experienced in python, any ideas why this isnt working? Thanks
getattrwhen working with classes, since the method is already stored in the dictionary of the class definition