Eman
Eman

Reputation: 83

String or object compairson in Python 3.52

I am working on the exorcism.io clock exercise and I can not figure out why this test is failing. The results look identical and even have the same type.

Here is my code:

class Clock:
    def __init__(self, h, m):
        self.h = h
        self.m = m
        self.adl = 0

    def make_time(self):
        s = self.h * 3600
        s += self.m * 60
        if self.adl: s += self.adl

        while s > 86400:
            s -= 86400

        if s == 0:
            return '00:00'

        h = s // 3600

        if h:
            s -= h * 3600

        m = s // 60
        return '{:02d}:{:02d}'.format(h, m)

    def add(self, more):
        self.adl = more * 60
        return self.make_time()

    def __str__(self):
        return str(self.make_time()) # i don't think I need to do this

if __name__ == '__main__':
    cl1 = Clock(34, 37) #10:37
    cl2 = Clock(10, 37) #10:37
    print(type(cl2))
    print(cl2, cl1)
    print(cl2 == cl1) #false

Upvotes: 3

Views: 92

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122372

A custom class without an __eq__ method defaults to testing for identity. That is to say, two references to an instance of such a class are only equal if the reference they exact same object.

You'll need to define a custom __eq__ method that returns True when two instances contain the same time:

def __eq__(self, other):
    if not isinstance(other, Clock):
        return NotImplemented
    return (self.h, self.m, self.adl) == (other.h, other.m, other.adl)

By returning the NotImplemented singleton for something that is not a Clock instance (or a subclass), you let Python know that the other object could also be asked to test for equality.

However, your code accepts values greater than the normal hour and minute ranges; rather than store hours and minutes, store seconds and normalise that value:

class Clock:
    def __init__(self, h, m):
        # store seconds, but only within the range of a day
        self.seconds = (h * 3600 + m * 60) % 86400
        self.adl = 0

    def make_time(self):
        s = self.esconds
        if self.adl: s += self.adl
        s %= 86400
        if s == 0:
            return '00:00'

        s, h = s % 3600, s // 3600
        m = s // 60
        return '{:02d}:{:02d}'.format(h, m)

    def __eq__(self, other):
        if not isinstance(other, Clock):
            return NotImplemented
        return (self.seconds, self.adl) == (other.seconds, other.adl)

Now your two clock instances will test equal because internally they store the exact same time in a day. Note that I used the % modulus operator rather than a while loop and subtracting.

Upvotes: 5

Related Questions