python - 在类方法中使用 __builtin__ 函数的属性错误

标签 python linux namespaces built-in

我在我的 arch linux box 上安装了 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 有更深入了解的人可以尝试一下。

谢谢 米

最佳答案

reduce(lambda x,y: x+y, l) 等同于 sum(l)。你能试试 sum(l) 是否有效吗?此外,您使用的是哪个 python 版本(sum 在版本 >= 2.3 中可用)

关于python - 在类方法中使用 __builtin__ 函数的属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450510/

相关文章:

python - 如何在 igraph 中提取某些路径类型?

python - 按类别计算精度和召回率

Linux chcon : can't apply partial context to unlabeled file

flash - Actionscript-3 命名空间和动态类型?

python - python 的可变长度参数 (*args) 会在函数调用时扩展生成器吗?

python - 如何在 lxml 中添加 python 关键字术语作为标记属性?

c++ - 如何检测重写虚方法声明中的 "missing" 'virtual' 说明符?

linux - 如何从 bash shell 获取输出

c++ - 使用 C++ 并尝试 "nest"一些代码

java - 升级没有 .proto 的 protobuf jar 的命名空间