Eval Keyword In Python

Introduction

In this article, we will see one of the major uses of eval. Eval is the inbuilt keyword available in Python. It is used to parse the expression and execute Python script or expression.

We will eval the keyword use case in the switcher case.

Below is the sample code of four functions. Which we are evaluating using switcher.get.

def add(a, b):
    print("Add function executed", a+b)
    return a + b


def sub(a, b):
    print("Sub function executed", a - b)
    return a - b


def mul(a, b):
    print("Mul function executed", a * b)
    return a*b


def div(a, b):
    if a==0 or b==0:
        print("Div function executed", 0)
        return 0 
    print("Div function executed", a/b)
    return a/b



def perform_math(a, b, func_symbol):

    switcher = {
        '+': add(a, b),
        '-': sub(a, b),
        '*': mul(a, b),
        "/" : div(a, b)
    }
    
    func = switcher.get(func_symbol, 0)
    print(f"Result found for operation:'{func_symbol}' for values {a} and {b} is", func)

print('-----------------------------------------------------------------------')
print('Normal code execution')
print('-----------------------------------------------------------------------')
perform_math(9, 3, "+")
print('-----------------------------------------------------------------------')

If we see the above code, which contains the normal way of code using switcher. Below is the sample output of the code

Now if we see the above code output. Even we are interested in only add functions and it executed all functions and in the end, it provided us the required result. It worked fully as expected but unnecessarily it executed another 3 functions as well. 

We can avoid executing other functions, using the eval function like below.

def perform_math_with_eval(a, b, func_symbol):

    switcher = {
        '+': 'add(a, b)',
        '-': 'sub(a, b)',
        '*': 'mul(a, b)',
        "/" : 'div(a, b)'
    }
    
    func = switcher.get(func_symbol, 0)
    print(f"Result found for operation:'{func_symbol}' for values {a} and {b} is", eval(func))

    
print()
print('-----------------------------------------------------------------------')
print('Code execution with the help of eval')
print('-----------------------------------------------------------------------')
perform_math_with_eval(9, 3, "+")
print('-----------------------------------------------------------------------')

Below is the output of the code,

Now if we observe the output of the above code, we see that the code is evaluating the function which is required. It skipped 3 functions and executed only one as in switcher case functions we are treating like strings (not like a function) after fetching the required function with the help of eval, we are evaluating the string-like python function/expression/script.

Summary

I hope you now understand the eval function when we are using switcher. From this, we will be able to avoid unnecessary statements.


Recommended Free Ebook
Similar Articles