0

I have a function like this:

def fog_create_user (i,j,k):pass
def fog_delect_user ():pass
def fog_update_user ():pass
def fiqa (i.j.k):pass
def single_match ():pass
def batch_match ():pass
def verify ():pass
def create_user ():pass
def delect_user ():pass
def update_user():pass


def switch_function(input_key):
    func_dict = {
        "fog_create_user":fog_create_user,
        "fog_delect_use":fog_delect_user,
        "fog_update_user":fog_update_user,
        "fiqa":fiqa,
        "single_match":single_match,
        "batch_match":batch_match,
        "verify":verify,
        "create_user":create_user,
        "delect_user":delect_user,
        "update_user":update_user
    }
    return func_dict.get(input_key)

how can I exec function and passing parameters like input_key = 'fiqa' exec fiqa(i,j.k)

1
  • desired_func = switch_function('fiqa'); desired_func(i,j,k) Commented Jan 10, 2020 at 8:16

3 Answers 3

1

You could call the function inline by adding the parameters afterwards

switch_function("fiqa")(1,2,3)

Or, if you are trying to to call the required function directly then this might be what you are looking for

def exec_function(input_key, *params):
    func_dict = {
        "fog_create_user":fog_create_user,
        "fog_delect_use":fog_delect_user,
        "fog_update_user":fog_update_user,
        "fiqa":fiqa,
        "single_match":single_match,
        "batch_match":batch_match,
        "verify":verify,
        "create_user":create_user,
        "delect_user":delect_user,
        "update_user":update_user
    }
    func = func_dict.get(input_key)
    return func(*params)

exec_function("fiqa",4,5,6)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

#take user input (assuming "fiqa")
function_name = "fiqa"

#returns the function
function_to_call = switch_function(function_name)

#call the function
function_to_call(i,j,k)

Comments

0

you can use reflections, if you wrap the functions into a class, you can use the function

getattr()

https://www.geeksforgeeks.org/python-getattr-method/

to call function as string parameter with its parameters

class YourClass():

    def __init__(self):pass
    def fog_create_user (i,j,k):pass
    def fog_delect_user ():pass
    def fog_update_user ():pass
    def fiqa (i.j.k):pass
    def single_match ():pass
    def batch_match ():pass
    def verify ():pass
    def create_user ():pass
    def delect_user ():pass
    def update_user():pass


def switch_function(input_key):
     obj = YourClass()
     for name in .__dict__.items() :
         if type(name) == FunctionType:
             getattr(obj, name)

Note: the code is not tested. The methods can also be called with parameters, additional checks need to be done, or make the parameters class attributesand give them default values.

2 Comments

This isn't a bad idea, but this answer does a terrible job at explaining it, and should perhaps be a comment instead.
The link sends to the explanation. Sure, probably as a comment might have sufficed, I'll keep it in mind for future responses

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.