Python中eval()和print()函数中的错误
Python中eval()和print()函数中的错误
该问题已经有答案了:
我试图在Python IDLE中运行以下代码:
>>> eval(print("123*13"))
我得到了正确的输出,但是它也伴随着一个TypeError
:
123*13 Traceback (most recent call last): File "<pyshell#4>", line 1, in eval(print("123*13")) TypeError: eval() arg 1 must be a string, bytes or code object
注意:
我不想使用print(eval(\"123*13\"))
。
我想在eval()
函数中使用print()
。
这不是一个实际的实现问题,但是在使用eval()
函数时我会遇到这个错误。我提出这个问题只是好奇而已。
admin 更改状态以发布 2023年5月21日
使用方法:
eval('print("123*13")')
在eval()函数的src中:
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函数帮助文档:
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中查看更多资料。