评估函数doen';在Python2.7中无法正常工作

评估函数doen';在Python2.7中无法正常工作,python,python-2.7,Python,Python 2.7,据我所知,eval函数允许python程序在其内部运行python代码 我想使用Python2.7中的eval函数运行print()命令,但收到以下错误: >>> print "test" test >>> command='print "test"' >>> command 'print "test"' >>> eval(command) Traceback (most recent call last): File

据我所知,eval函数允许python程序在其内部运行python代码

我想使用Python2.7中的
eval
函数运行
print()
命令,但收到以下错误:

>>> print "test"
test
>>> command='print "test"'
>>> command
'print "test"'
>>> eval(command)

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    eval(command)
  File "<string>", line 1
    print "test"
        ^
SyntaxError: invalid syntax
>>> 
在Python 2中,是一个语句,而不是一个表达式。用于计算表达式;您可以使用来执行语句

在Python3中,是一个函数调用,因此可以对其求值。

在Python2中是一个语句,而不是表达式。用于计算表达式;您可以使用来执行语句

在Python 3中,是一个函数调用,因此可以对其求值

>>> print ("test")
test
>>> command='print("test")'
>>> command
'print("test")'
>>> eval(command)
test
>>>