What does Python's eval() do?

前端 未结 10 2076
后悔当初
后悔当初 2020-11-22 01:48

In the book that I am reading on Python, it keeps using the code eval(input(\'blah\'))

I read the documentation, and I understand it, but I still do no

10条回答
  •  北海茫月
    2020-11-22 02:42

    eval(), as the name suggests, evaluates the passed argument.

    raw_input() is now input() in python 3.x versions. So the most commonly found example for the use of eval() is its use to provide the functionality that input() provided in 2.x version of python. raw_input returned the user-entered data as a string, while input evaluated the value of data entered and returned it.

    eval(input("bla bla")) thus replicates the functionality of input() in 2.x, i.e., of evaluating the user-entered data.

    In short: eval() evaluates the arguments passed to it and hence eval('1 + 1') returned 2.

提交回复
热议问题