我猜这是一个问题,只有那些喜欢挑战的人谁挖到源代码的Python溯源。。。但也许有人知道答案从他们的头顶
这应该很容易复制,请参阅下面的代码(我认为,根据您的硬件和sys.setrecursionlimit()的值,您可能需要从我的值2000增加最大迭代次数)
它是numpy.genfromtxt读取一个CSV文件,该文件包含一列和一行,由单个字符0组成。当显式设置“converters”(在下面注释掉)时,一切都很好而且很快。当如代码所示间接设置“converters”时,Python执行的是完全不必要的递归操作,代码在1400到1500次迭代之间失败(在我的计算机上),错误为“RecursionError:maximum recursion depth exceeded”。在代码失败之前,随着迭代(可能还有递归深度)的增加,它变得越来越慢。回溯指向相关的源代码,但我不知道如何深入研究
问题是:为什么这个代码不能像显式设置“转换器”的代码那样工作?是虫子,还是有道理;i、 我的代码不好
#Spyder 3.3.3 | Python 3.7.3 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10
import numpy as np
the_converters = {'data': lambda s : 0}
jcount = 0
while jcount < 2000:
jcount = jcount + 1
print(jcount)
the_array = np.genfromtxt('recursion_debug.csv', delimiter =',', \
names = 'data', \
converters = the_converters, \
#converters = {'data': lambda s : 0}, \
)
genfromtxt
正在修改cvt
对象(就地),此效果是累积的:请注意,指定的键值不会更改;相反,它使用修改后的转换器添加了一个列编号键
相反,如果我们在线创建字典,并且只提供lambda(或函数),则不会修改函数:
现在创建一个函数,同时显示输入字符串:
如果我指定
encoding
,字典仍会被修改(新键),但函数不会被修改:在没有编码(或默认的“bytes”)的情况下,添加
tobytes
包装器,并将bytestring传递给我的函数:===
添加
functools.partial
的代码是旧的Py2到Py3字节到unicode开关的一部分:相关问题 更多 >
编程相关推荐