3

I am trying to implement a standard equality operator in Python 3.3, following code samples from other questions. I'm getting an assertion error, but I can't figure out what's broken. What did I miss here?

class RollResult:
    def __init__(self, points, unscored_dice):
        self.points = points
        self.unscored_dice = unscored_dice

    def __eq__(self, other):
        return (self.points == other.points and self.unscored_dice == other.unscored_dice)

And here's the test. Many other tests are passing, so the basic setup is right. This is my first test of the class and I've never tried unit testing equality overloads before, so it may be the fault of the test as well.

class TestRollResultClass(unittest.TestCase):
    def test_rollresult_equality_overload_does_not_test_for_same_object(self):
        copy1 = RollResult(350,2)
        copy2 = RollResult(350,2)
        self.assertNotEqual(copy1,copy2)

Result:

AssertionError: <greed.RollResult object at 0x7fbc21c1b650> == <greed.RollResult object at 0x7fbc21c1b650>                                        
7
  • 2
    Why are you asserting that two things constructed to be equal aren't? Commented Nov 12, 2013 at 19:31
  • 1
    Note that your method is wrong. The argument other of __eq__ might not be a RollResult instance, in which case you should return False and not raise an AttributeError (people expect to be able to use == between any objects, except for really strange cases which should be well documented). I suggest you to insert the code in a try: ... except AttributeError: return False.
    – Bakuriu
    Commented Nov 12, 2013 at 19:33
  • @Bakuriu Thanks for the feedback. I've seen a lot of people say it's bad to go around checking types all the time. Is this a usual exception to that standard (lack of) practice? Commented Nov 12, 2013 at 19:38
  • Yes, it is bad practice (in python) to check the types, which means using isinstance or doing things like type(something) is SomeType etc. Using Duck-typing is encouraged. Duck-typing means: you assume the object provide a certain interface, and use it. If it fails (like in this case, with an AttributeError) you either fail the operation (possibly raising a different exception, or sometimes letting the exception through), or you continue assuming a different interface. In this case the __eq__ should first assume a "compatible interface", and otherwise fail returning False.
    – Bakuriu
    Commented Nov 12, 2013 at 19:42
  • 2
    @Bakuriu: Actually, if other isn't a RollResult, you should return NotImplemented. This is generally true even if you don't expect other objects to implement a meaningful equality comparison with RollResult objects. Commented Nov 12, 2013 at 23:34

1 Answer 1

5

Your __eq__() seems to be working correctly. You are using assertNotEqual(), which will raise the AssertionError if the two arguments are equal. You provided the same arguments to each RollResult object used in the assertion so they are equal, hence the failure.

It looks like you either want to be using assertEqual(), or change it so that copy1 and copy2 are constructed differently.

4
  • 1
    In short, I tested exactly the opposite of what I meant to. headdesk That does clear it up. Commented Nov 12, 2013 at 19:32
  • Happens to us all from time to time :) Commented Nov 12, 2013 at 19:33
  • Often it is auto-completion's fault... (at least in my case).
    – Bakuriu
    Commented Nov 12, 2013 at 19:34
  • Usually I'd have figured this out, but I am new at both Python and unit testing, so I thought the failure notice was a more serious error that would pop up in actual practice. Now I Know. Commented Nov 12, 2013 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.