1

我知道当我比较两个对象lhs == rhs并且都定义__eq__时,lhs.__eq__除非它返回NotImplemented或者rhslhs.

但是,我想实现一个类,它的实例在与任意对象进行比较时,将有机会说出他们想说的话,而arbitrary_object.__eq__不管比较语句的实现细节和位置。这听起来有点尴尬,但我正在做一个面向测试的小项目,看看testmania.expect你就会明白我需要这个做什么。

__instancecheck__我最初的想法是使用元类魔法和,使我的类成为任何其他类的子类__subclasscheck__。但在简单比较的情况下,它们根本不会被调用。

有人有什么新鲜想法吗?

4

1 回答 1

1

我不知道这是否适合您的需要,但是为什么不测试这两个操作,我的意思是测试 if :object1 == object2并且object2 == object1通常您应该得到相同的值,除非其中一个对象覆盖了该__eq__方法,因此您将执行这个新__eq__方法并返回一个正确的方法,一个例子胜于言语:

def _assert_just_now(first, second):
    """A Dump function to simulate if two dates are almost equal.

    N.B: In this Dump function i will just test if the two datetime object have the
    same hour

    """ 

    from datetime import datetime
    assert isinstance(first, datetime) and isinstance(second, datetime), \
           "This function only accept datetime objects"

    return first.hour == second.hour

class Expectation(object):

     def __init__(self, assertion, first):
         self.assertion = assertion
         self.first = first

     def __eq__(self, other):
         return self.assertion(self.first, other)

def assert_equal(first, second):
   """Usage :

   >>> from datetime import datetime
   >>> t1 = datetime(year=2007, hour=1, month=3, day=12)
   >>> t2 = datetime(year=2011, hour=1, month=5, day=12)

   Without using Expectation it's False.
   >>> assert_equal(t1, t2)
   False

   Use the Expectation object.
   >>> assert_equal(t1, Expectation(_assert_just_now, t2))
   True

   Can use Expectation in the first argument too.
   >>> assert_equal(Expectation(_assert_just_now, t2), t1)
   True

   Work also in Container object.
   >>> assert_equal({'a': 1, 'b': Expectation(_assert_just_now, t2)},
   ...              {'a': 1, 'b': t1})
   True

   We change a little bit the values to make the assert equal fail.
   >>> t3 = datetime(year=2011, hour=2, month=5, day=12)
   >>> assert_equal(t1, t3)
   False

   This just to make sure that the _assert_just_now doesn't accept object 
   other than datetime:
   >>> assert_equal(t1, Expectation(_assert_just_now, "str"))
   Traceback (most recent call last):
       ...
   AssertionError: This function only accept datetime objects

   """

   return first == second or second == first

if __name__ == '__main__':
   import doctest
   doctest.testmod() 

希望这能有所帮助。

于 2011-03-13T16:11:23.490 回答