下面是我的代码片段。当我运行程序时,它会给出以下错误。
@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)导入语句中缺少什么东西吗?
发布于 2014-12-18 03:03:53
不,你的进口证明没问题。问题是Python安装落后于一个版本。functools.total_ordering是在Python3.2中添加的。来自文档
新版本3.2。 在版本3.4中更改:现在支持从未识别类型的底层比较函数返回
NotImplemented。
因此,为了使用它,您将需要进行升级。如果这是不可能的,那么您只需手动定义所有比较运算符。
注意,这个装饰器也被移植到Python2.7,但我假设您希望继续使用Python3.x。
https://stackoverflow.com/questions/27538577
复制相似问题