skip to Main Content

For a school project about artificial intelligence, I need to be able to connect a function name to another function like this for example:

def b_test(x):
    return x+1

def a_test(x):
    return x

variable = "b"
variable+= "_test"
a_test = variable

But this happenned

I guess that Python wants to use the name “b” as the new name/alias of the function a_test instead of using b_test as the new name.

So how can I force python to look what b means instead of just using it?

EDIT:

What I want to do is :

I have in my variable variable the string b and I need to, when I use in my code a_test(x), return x+1 (the return of the function b_test). So I add the string “_test” to variable so it is “b_test”.

I could do it by coding

a_test = b_test

but I won’t only connect a_test to b_test, it might be c_test or d_test, that’s why I need to use a variable.

2

Answers


  1. Your example should work actually, but it doesn’t reflect your actual code and error from the screenshot. In your screenshot, you are trying to get the ChosenFunction from mlp_functions module, which does not have that attribute, hence the AttributeError. To get the attribute from a module using a str name, you would need to use getattr like:

    mlp_functions.logistic_function = getattr(mlp_functions, ChosenFunction)
    
    Login or Signup to reply.
  2. First of all you need to construct the function name (from your variable) and retrieve the module name so you can load the python function object in the variable you need.

    Here is the code:

    def b_test(x):
        return x + 1
    
    
    def a_test(x):
        return x
    
    
    def main():
        # Construct function name
        test_option = "b"  # "a"                                                                                                                                                                                                                                                   
        test_name = test_option + "_test"
    
        # Retrieve function module
        import os
        import importlib
        module_name = os.path.splitext(os.path.basename(__file__))[0]
        module = importlib.import_module(module_name)
    
        # Retrieve function
        test = getattr(module, test_name)
    
        # Debug information
        if __debug__:
            print ("Current file: " + str(__file__))
            print ("Module name: " + str(module_name))
            print ("Module: " + str(module))
            print ("Test function name: " + str(test_name))
            print ("Test function: " + str(test))
    
        # Execute function
        result = test(1)
        print ("Result: " + str(result))
    
    
    if __name__ == '__main__':
        main()
    

    Some important notes:

    • The test option will be just a string containing the test function you want to load.
    • Instead of “renaming” the a_test function, just use a test variable that points to the correct function to execute
    • The getattr function is a little bit tricky with file names and modules so if you modify this code you maybe want to modify the way the function module is computed.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search