2024-09-30 14:36:07 发布
网友
我正在阅读瓶子源代码,看到:
eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<py3fix>', 'exec'))
我阅读了关于compile的文档,它只告诉我<string>是常用的。我也搜索了stackoverflow,但找不到相关信息。你知道吗
compile
<string>
有人能告诉我<py3fix>是如何影响编译的吗?还有其他文件名吗?我在哪里可以找到相关的文档?你知道吗
<py3fix>
提前谢谢。你知道吗
一点也不影响。它只是一个名称,用于标识编译代码的来源,因此可以使用所需的字符串。你知道吗
就像医生说的:
compile(source, filename, mode[, flags[, dont_inherit]])The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('< string>' is commonly used).
compile(source, filename, mode[, flags[, dont_inherit]])
The filename argument should give the file from which the code was read; pass some recognizable value if it wasn’t read from a file ('< string>' is commonly used).
在没有从文件中读取source的情况下(如这里),他们建议您使用<string>,这样您就知道这段代码是从一个已写入的字符串编译的。你知道吗
source
编写代码的人,是在修复一些Bottle Python 2/3 bugs时编写的。所以我猜他用<py3fix>作为一种方法来确定断言是从用户运行2.x时他编译的def _raise中产生的:
def _raise
>>> eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<py3fix>', 'exec')) >>> _raise(Exception, "error message", None) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<py3fix>", line 1, in _raise Exception: error message >>> eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<my_source_file>', 'exec')) >>> _raise(Exception, "error message", None)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<my_source_file>", line 1, in _raise Exception: error message
一点也不影响。它只是一个名称,用于标识编译代码的来源,因此可以使用所需的字符串。你知道吗
就像医生说的:
在没有从文件中读取
source
的情况下(如这里),他们建议您使用<string>
,这样您就知道这段代码是从一个已写入的字符串编译的。你知道吗编写代码的人,是在修复一些Bottle Python 2/3 bugs时编写的。所以我猜他用
<py3fix>
作为一种方法来确定断言是从用户运行2.x时他编译的def _raise
中产生的:相关问题 更多 >
编程相关推荐