• Tidak ada hasil yang ditemukan

Flexible User Input with eval and exec

5.2 Flexible User Input withevalandexec 61

Here, the liner = eval(s)is equivalent to writingr = 1+2, but usingeval gives much more flexibility, of course, since the string is stored in a variable and can be read as input.

A small Python program using eval can be quite flexible. Consider, for instance, the following code

i1 = eval(input(’operand 1: ’)) i2 = eval(input(’operand 2: ’)) r = i1 + i2

print(f’{type(i1)} + {type(i2)} becomes {type(r)} with value{r}’)

This code can handle multiple input types. If we save the code in a file add_input.pyand run it from the terminal, we can, for instance, add integer and float numbers, as in:

Terminal

Terminal> python add_input.py operand 1: 1

operand 2: 3.0

<type ’int’> + <type ’float’> becomes <type ’float’>

with value 4

or two lists, as follows:

Terminal

Terminal> python add_input.py operand 1: [1,2]

operand 2: [-1,0,1]

<type ’list’> + <type ’list’> becomes <type ’list’>

with value [1, 2, -1, 0, 1]

We could achieve similar flexibility with conventional type conversion, that is, usingfloat(i1),int(i1), and so on, but that would require much more programming to correctly process the input strings. Theevalfunction makes such flexible input handling extremely compact and efficient, but it also quickly breaks if the input is slightly wrong. Consider the following examples:

Terminal

Terminal> python add_input.py operand 1: (1,2)

operand 2: [3,4]

Traceback (most recent call last):

File "add_input.py", line 3, in <module>

r = i1 + i2

TypeError: can only concatenate tuple (not "list") to tuple Terminal> python add_input.py

5.2 Flexible User Input withevalandexec 63 operand 1: one

Traceback (most recent call last):

File "add_input.py", line 1, in <module>

i1 = eval(input(’operand 1: ’)) File "<string>", line 1, in <module>

NameError: name ’one’ is not defined

In the first of these examples, we try to add a tuple and a list, which one could easily imagine would work, but Python does not allow this and there- fore the program breaks. In the second example, we try to make the program add two strings, which usually works fine; for instance "one" +"one" be- comes the string "oneone". However, the eval function breaks when we try to input the first string. To understand why, we need to think about what the corresponding line really means. We try to make the assignment i1 = eval(’one’), which is equivalent to writing i1 = one, but this line does not work unless we have already defined a variable named one. A remedy to this problem is to input the strings with quotation marks, as in the following example

Terminal

Terminal> python add_input.py operand 1: "one"

operand 2: "two"

<class ’str’> + <class ’str’> becomes <class ’str’>

with value onetwo

These examples illustrate the benefits of theevalfunction, and also how it easily breaks programs and is generally not recommended for "real programs".

It is useful for quick prototypes, but should usually be avoided in programs that we expect others to use or that we expect to use ourselves over a longer time frame.

The other "magic" text handling function is namedexec, and it is fairly similar to eval. However, whereas eval evaluates an expression, exec exe- cutes a string argument as one or more complete statements. For instance, if we define a strings = "r = 1+1",eval(s)is illegal, since the value of s ("r = 1+1") is a statement (an assignment), and not a Python expression.

However, exec(s) will work fine and is the same as including the line r = 1+1directly in the code. The following code illustrates the difference:

expression = ’1+1’ #store expression in a string statement = ’r = 1+1’ # store statement in a string q = eval(expression)

exec(statement)

print(q,r) # results are the same

We can also use exec to execute multiple statements, for instance using multi-line strings:

somecode = """

def f(t):

term1 = exp(-a*t)*sin(w1*x) term2 = 2*sin(w2*x)

return term1 + term2

"""

exec(somecode) # execute the string as Python code

Here, theexecline will simply execute the stringsomecode, just as if we had typed the code directly in our program. After the call toexec we have de- fined the functionf(t)and can call this function in the usual way. Although this example does not seem very useful, the flexibility of execbecomes more apparent if we combine it with actual user input. For instance, consider the following code, which asks the user to type a mathematical expression in- volvingxand then embeds this expression in a Python function:

formula = input(’Write a formula involving x: ’) code = f"""

def f(x):

return {formula}

"""

from math import * # make sure we have sin, cos, log, etc.

exec(code) # turn string formula into live function

#Now the function is defined, and we can ask the

#user for x values and evaluate f(x) x = 0

while x is not None:

x = eval(input(’Give x (None to quit): ’)) if x is not None:

y = f(x)

print(f’f({x})={y}’)

While the program is running, the user is first asked to type a formula, which becomes a function. Then the user is asked to inputxvalues until the answer isNone, and the program evaluates the functionf(x)for eachx. The program works even if the programmer knows nothing about the user’s choice off(x) when the program is written, which demonstrates the flexibility offered by theexec andevalfunctions.

To consider another example, say, we want to create a programdiff.py that evaluates the numerical derivative of a mathematical expression f(x) for a given value ofx. The mathematical expression and the xvalue will be given as command line arguments. The program could be used as follows:

Terminal

Terminal> python diff.py ’exp(x)*sin(x)’ 3.4 Numerical derivative: -36.6262969164

5.3 Reading Data from Files 65