在类方法中使用 __builtin__ 函数出现属性错误
我在我的 arch linux 机器上安装了 rdiff-backup 结果却出现属性错误:
AttributeError: 'module' object has no attribute 'reduce'
错误存在于 rdiff_backup 类之一中,但我无法发现错误。函数 reduce
应该是内置的,我无法获取找到该函数的代码。
rdiff 的代码如下所示:
def get_total_dest_size_change(self):
"""Return total destination size change
This represents the total change in the size of the
rdiff-backup destination directory.
"""
addvals = [self.NewFileSize, self.ChangedSourceSize,
self.IncrementFileSize]
subtractvals = [self.DeletedFileSize, self.ChangedMirrorSize]
for val in addvals + subtractvals:
if val is None:
result = None
break
else:
def addlist(l): return reduce(lambda x,y: x+y, l)
result = addlist(addvals) - addlist(subtractvals)
self.TotalDestinationSizeChange = result
return result
错误发生在本地定义的 addlist 函数中。 我尝试在文件顶部导入内置模块(statistics.py),就像
import __builtin__
和
from __builtin__ import reduce
一样尝试更改方法的命名空间,如下所示:
def addlist(l): return __builtin__.reduce(lambda x,y: x+y, l)
但是唉。还是同样的错误。
到目前为止,我还没有找到任何好的信息或解决方案,所以也许对 python 有更深入了解的人可以尝试一下。
谢谢 米
I installed rdiff-backup on my arch linux box only to end up with the attribute error:
AttributeError: 'module' object has no attribute 'reduce'
The error exists in one of rdiff_backup classes, but I can not spot the error. The function reduce
should be builtin, and I can not get the code to find the function.
The code from rdiff looks like this:
def get_total_dest_size_change(self):
"""Return total destination size change
This represents the total change in the size of the
rdiff-backup destination directory.
"""
addvals = [self.NewFileSize, self.ChangedSourceSize,
self.IncrementFileSize]
subtractvals = [self.DeletedFileSize, self.ChangedMirrorSize]
for val in addvals + subtractvals:
if val is None:
result = None
break
else:
def addlist(l): return reduce(lambda x,y: x+y, l)
result = addlist(addvals) - addlist(subtractvals)
self.TotalDestinationSizeChange = result
return result
And the error occurs in the locally defined addlist function.
I've tried to import the builtin module in the top of the file (statistics.py), both like
import __builtin__
and
from __builtin__ import reduce
and tried to change the namespace of the method like so:
def addlist(l): return __builtin__.reduce(lambda x,y: x+y, l)
But alas. Still the same error.
I've not been able to find any good information or solution so far, so maybe someone with a little tighter knowledge about python could take stab at it.
Thanks
m
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
reduce(lambda x,y: x+y, l)
相当于sum(l)
。您可以尝试一下sum(l)
是否有效吗?另外,您使用的是哪个 python 版本(sum
在版本 >= 2.3 中可用)reduce(lambda x,y: x+y, l)
is an equivalent ofsum(l)
. Can you try whethersum(l)
works? Also, which python version are you using (sum
is available in version >= 2.3)