Tharanga Abeyseela
Tharanga Abeyseela

Reputation: 3483

AttributeError functools.total_ordering

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 ?

Upvotes: 1

Views: 1751

Answers (1)

user2555451
user2555451

Reputation:

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.

Upvotes: 2

Related Questions