From 38947e0934c1600fddd3f3b08b84fb5fc85caa12 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 8 Apr 2017 12:31:53 +0800 Subject: [PATCH] fix: unexpected behavior with ``!=`` operator. (fixes #26) --- README.rst | 14 ++++++++++++++ colour.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/README.rst b/README.rst index e90c90b..2c743e1 100644 --- a/README.rst +++ b/README.rst @@ -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, @@ -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 ------------------------------------------- diff --git a/colour.py b/colour.py index 84500eb..b5bc760 100644 --- a/colour.py +++ b/colour.py @@ -38,6 +38,8 @@ import hashlib import re +import sys + ## ## Some Constants @@ -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: @@ -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