0

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

2
  • You are calling the function in the definition of the dispatcher. You should assign the values to just the function like you did in the first example. Commented Jul 29, 2019 at 19:33
  • it is better to use getattr when working with classes, since the method is already stored in the dictionary of the class definition Commented Jul 29, 2019 at 19:39

4 Answers 4

3

Essentially, your class will act like a dict here, and your instance is the dispatcher so just use getattr and the self magic will work:

>>> class Foo:
...    def bar(self):
...       return 42
...    def baz(self):
...       return 88
...
>>> foo = Foo()
>>> getattr(foo, 'bar')()
42
>>> getattr(foo, 'baz')()
88
>>>
Sign up to request clarification or add additional context in comments.

Comments

1

There are several ways you could approach this problem, one of which would be to make dispatcher an instance method of the class:

class Foo(object):
     def __init__(self,lv,name):
         self.lv = lv
         self.name = name
     def one(self):
         print('this is one!')
     def dispatcher(self, action):
         d = {'one':self.one}
         return d[action]

Then you get something like:

>>> f = Foo(5, 'Joshua')
>>> action = input()
>>> f.dispatcher(action)()
'this is one!'

Comments

1

I can only guess what you are trying to do, but this snippet dispatches to the respective functions defined in your dispatcher for the user inputs one, two and three:

class femblem:
    def __init__(self, lv, name):
        self.lv = lv
        self.name = name

    def one(self):
        print('one hahah')

    def two(self):
        print('two hahah')

    def the_count(self):
        print('I am the count who likes to count')

    @property
    def dispatcher(self):
        return {
            'one': self.one, 
            'two': self.two, 
            'three': self.the_count
        }


action = input('Character: - ')        
unit1 = femblem(5, "joshua")
unit1.dispatcher[action]()

Comments

1

An alternative answer with getattr inside the class:

class Foo:
    def __init__(self):
        pass

    def bar(self):
        print('this is foo')

    def dispatcher(self):
        att = input('put some input: ')
        getattr(self, att)()

ob = Foo()
ob.dispatcher()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.