AttributeError functools.total_ordering

1.7k views Asked by At

Following is my code snippet. when i run the program it gives me the following error.

 @functools.total_ordering
 AttributeError: 'module' object has no attribute 'total_ordering'

I'm using 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)

Is something missing from the import statement ?

1

There are 1 answers

4
AudioBubble On BEST ANSWER

No, your import statement is fine. The problem is that your Python installation is one version behind. functools.total_ordering was added in Python 3.2. From the docs:

New in version 3.2.

Changed in version 3.4: Returning NotImplemented from the underlying comparison function for unrecognized types is now supported.

So, you will need to upgarde in order to use it. If that is not possible, then you will just have to define all of the comparison operators manually.

Note that this decorator was also backported to Python 2.7, but I assume that you want to keep with Python 3.x.