Python AttributeError functools.total_排序

Python AttributeError functools.total_排序,python,python-3.1,Python,Python 3.1,下面是我的代码片段。当我运行程序时,它会给我以下错误 @functools.total_ordering AttributeError: 'module' object has no attribute 'total_ordering' 我正在使用python 3.1 import functools @functools.total_ordering class Abs(object): def __init__(self,num): self.num=abs(

下面是我的代码片段。当我运行程序时,它会给我以下错误

 @functools.total_ordering
 AttributeError: 'module' object has no attribute 'total_ordering'
我正在使用python 3.1

import functools

@functools.total_ordering
class Abs(object):
    def __init__(self,num):
        self.num=abs(num)
    def __eq__(self,other):
        return self.num==abs(other.num)
    def __lt__(self,other):
        return self.num < abs(other.num)

five=Abs(-5)
four=Abs(-4)
print(five > four)
导入工具
@functools.total_排序
Abs类(对象):
定义初始化(self,num):
self.num=abs(num)
定义(自身、其他):
return self.num==abs(other.num)
定义(自身、其他):
返回self.num四张)

导入语句中缺少什么内容吗?

不,您的导入语句没有问题。问题是您的Python安装落后了一个版本<代码>functools.total_ordering添加到Python 3.2中。从:

3.2版中的新版本

在版本3.4中更改:从底层返回
NotImplemented
现在支持用于无法识别类型的比较函数

因此,您需要升级才能使用它。如果不可能,则只需手动定义所有比较运算符


请注意,这个装饰器也被后传到了Python2.7,但我假设您希望继续使用Python3.x。

我可以在这里看到总的排序,是的
total_ordering
被后传到Python 2.7。所以,如果你愿意,你可以使用这个版本。请记住,Python3.1是在2.7成为今天的样子之前出现的。Python社区现在基本上是分裂的。一些正在开发2.7,而另一些正在开发3.5.:)升级到2.7.9,但它现在给了我一个不同的错误。文件“/1.py”,第14行,在x=(五个>四个)文件“/usr/local/lib/python2.7/functools.py”,第56行,在'lt':[('gt',lambda self,other:not(selfAbs()一个
Abs
实例时该怎么做。看起来您应该在比较方法中执行
Abs(other.num)
。否则,您将需要重载
Abs
来告诉您在Python类上使用
Abs()
时要做什么。