Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to eliminate a python3 deprecation warning for the equality operator?

Although the title can be interpreted as three questions, the actual problem is simple to describe. On a Linux system I have python 2.7.3 installed, and want to be warned about python 3 incompatibilities. Therefore, my code snippet (tester.py) looks like:

#!/usr/bin/python -3

class MyClass(object):    
    def __eq__(self, other):
        return False

When I execute this code snippet (thought to be only to show the problem, not an actual piece of code I am using in my project) as

./tester.py

I get the following deprecation warning:

./tester.py:3: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class MyClass(object):

My question: How do I change this code snippet to get rid of the warning, i.e. to make it compatible to version 3? I want to implement the equality operator in the correct way, not just suppressing the warning or something similar.

like image 747
Alex Avatar asked Mar 18 '13 07:03

Alex


1 Answers

From the documentation page for Python 3.4:

If a class does not define an __eq__() method it should not define a __hash__() operation either; if it defines __eq__() but not __hash__(), its instances will not be usable as items in hashable collections. If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

Basically, you need to define a __hash()__ function.

The problem is that for user-defined classes, the __eq()__ and __hash()__ functions are automatically defined.

x.__hash__() returns an appropriate value such that x == y implies both that x is y and hash(x) == hash(y).

If you define just the __eq()__, then __hash()__ is set to return None. So you will hit the wall.

The simpler way out if you don't want to bother about implementing the __hash()__ and you know for certain that your object will never be hashed, you just explicitly declare __hash__ = None which takes care of the warning.

like image 183
Sudipta Chatterjee Avatar answered Sep 30 '22 14:09

Sudipta Chatterjee