0

I have a file three_ways.py. There I have 3 functions for calculating 3 different y.

# The content of error_metrics.py

def way_1(x1, x2):
    y = x1 - x2
    return y

def way_2(x1, x2)
    y = x1 ** 2 - x2 ** 2 
    return y

def way_3(x1, x2)
    y = x1 * x2
    return y

In my main (another) py-file, I want to design a function get_y(). It accepts a string argument to control which way to use.

way = 'way_2'
y = get_y(x1, x2, way)

I could use if-elif statement, see below. But I want avoid using if-elif. Because there can be arbitrary number of functions of different way_n(), n can be any number. Whenever I add a way_x() function in the file three_ways.py, I will have to make another elif statement in the function get_y(). And when n gets larger, the whole codes will be hard to read.

# starting point for making the function of get_y()
import three_ways

def get_y(x1, x2, way):
    if way == 'way_1':
        y = three_ways.way_1(x1, x2)
    elif way == 'way_2':
        y = three_ways.way_2(x1, x2)
    elif way == 'way_3':
        y = three_ways.way_3(x1, x2)
    else:
        print('Not implemented yet')
    return y

So could you please help me with building such a function? It should be efficient, elegant, pythonic, and no if-elif statement. I am using Python3.

Thanks in advance.

2
  • 1
    Pass the function you want to call as an argument :) You can introspect in python so you can lookup the method you want in the package. But since you already know the "way" just pass it in or call it. Commented Sep 15, 2017 at 14:23
  • 1
    @Rob, thanks for your suggestion. My issue is I need to use a string in my main file to specify which way_x() to call. So I cannot pass the function as an argument. Because I am designing an user interface. The user will type the function name, and the program will read the typed name as string, and call the corresponding function. Commented Sep 15, 2017 at 19:51

1 Answer 1

1

If you really want to do exactly what you described, you can look the function up using getattr:

way_func = getattr(three_ways, way)
y = way_func(x1, x2)

But because Python supports first-class-functions (functions can be passed around just like other data types), it's preferable to do what Rob said, which is just passing the function itself:

def get_y(x1, x2, way_func):
    return way_func(x1, x2)
get_y(4, 2, three_ways.way_2)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your help. My issue is I need to use a string in my main file to specify which way_x() to call. So I cannot pass the way_func as an argument. Because I am designing an user interface. The user will type the function name, and the program will read the typed name as string, and call the corresponding function.

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.