Skip to content

Commit

Permalink
fix: unexpected behavior with != operator. (fixes #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaab committed Apr 8, 2017
1 parent 69f5add commit 38947e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.rst
Expand Up @@ -296,6 +296,8 @@ But on another non-Colour object::

>>> red == None
False
>>> red != None
True

Actually, ``Colour`` instances will, politely enough, leave
the other side of the equality have a chance to decide of the output,
Expand All @@ -313,6 +315,18 @@ the other side of the equality have a chance to decide of the output,
>>> blue == alien_red
False

And inequality (using ``__ne__``) are also polite::

>>> class AnotherColorImplem(OtherColorImplem):
... def __ne__(self, other):
... return self.color != other.web

>>> new_alien_red = AnotherColorImplem("red")
>>> red != new_alien_red
False
>>> blue != new_alien_red
True


Picking arbitrary color for a python object
-------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions colour.py
Expand Up @@ -38,6 +38,8 @@

import hashlib
import re
import sys


##
## Some Constants
Expand Down Expand Up @@ -931,6 +933,12 @@ class Color(object):
>>> Color('red') == Color('blue')
False
>>> Color('red') == Color('red')
True
>>> Color('red') != Color('blue')
True
>>> Color('red') != Color('red')
False
But this can be changed:
Expand Down Expand Up @@ -1091,6 +1099,12 @@ def __eq__(self, other):
return self.equality(self, other)
return NotImplemented

if sys.version_info[0] == 2:
## Note: intended to be a backport of python 3 behavior
def __ne__(self, other):
equal = self.__eq__(other)
return equal if equal is NotImplemented else not equal


RGB_equivalence = lambda c1, c2: c1.hex_l == c2.hex_l
HSL_equivalence = lambda c1, c2: c1._hsl == c2._hsl
Expand Down

0 comments on commit 38947e0

Please sign in to comment.