1
possibleRequests = ['test', 'test1']

def inboxReader():
        global inbox
        tempInbox = []
        tempInbox = inbox.inboxMessage #inboxMesage remains filled?
        print(inbox.inboxMessage, 'inboxReader')
        i = 0
        while (i < len(tempInbox)):
            if (tempInbox[i] in possibleRequests):
                print('THIS IS WORKING')
            #print(i)
            i+=1

I want to be able to have possible requests point towards a method to run rather than have a long list of if statments. What am I able to do in order to have a variable point towards and run a method.

Cheers,

Marc

3
  • I would like more info, what is inbox.inboxMessage? a list? where do you want to "have possible requests point towards a method to run rather than have a long list of if statments"? Commented Jun 18, 2016 at 3:49
  • You can store functions (and methods) in variables (e.g. f = inboxReader; f()).. Commented Jun 18, 2016 at 3:55
  • inbox is a class that I call its an array. how would I go about doing what thebjorn said? it seams to be the thing i'm looking for Commented Jun 18, 2016 at 4:01

1 Answer 1

2

You can first create a dictionary of functions then refer to it with tempInbox[i]. Example code below:

def func_a(x):
    return x

def func_b(x):
    return x*10

tempInbox = (2,3)

fn_dict = {"a":func_a,"b":func_b}
print fn_dict["a"](tempInbox[0]) # returns 2
print fn_dict["b"](tempInbox[1]) # returns 30
Sign up to request clarification or add additional context in comments.

1 Comment

awesome this is exactly what I need

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.