python 中带有打印函数的 eval() 函数出错

Error with eval() function with print function in python

我正尝试 运行 在 Python 空闲中执行此操作:

>>> eval(print("123*13"))

我得到了正确的输出,但它带有 TypeError:

123*13
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    eval(print("123*13"))
TypeError: eval() arg 1 must be a string, bytes or code object

注意:
不想 print(eval("123*13"))
我想在 eval() 函数中使用 print()。 这不是针对实际实现的,但是我在使用 eval() 函数时遇到了这个错误。我不是要求任何实际实施,而是出于好奇。

使用它:

eval('print("123*13")')

在 eval() 源代码中:

evel (__source: str | bytes | CodeType, __globals: Dict[str, Any] | None = ..., __locals: Mapping[str, Any] | None = ...) -> Any
'''Evaluate the given source in the context of globals and locals.

The source may be a string representing a Python expression    
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.'''
...

评估帮助():

eval(source, globals=None, locals=None, /)
    Evaluate the given source in the context of globals and locals.

    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.

您可以在 Python.org 中查看更多内容。