在类方法中使用 __builtin__ 函数出现属性错误

发布于 2024-10-26 17:54:00 字数 1411 浏览 0 评论 0原文

我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-11-02 17:54:00

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 of sum(l). Can you try whether sum(l) works? Also, which python version are you using (sum is available in version >= 2.3)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文