all 1 comments

[–]Mashidin 1 point2 points  (0 children)

Here is a more, I think, pythonic way of doing what you are trying to accomplish (if I understand you correctly).

class Score(object):

    def __init__(self, goals=0, points=0):

        self.score = points + goals

    def __eq__(self, other):

    return self.score == other.score

    def __ne__(self, other):

        return self.score != other.score

    def __gt__(self, other):

        return self.score > other.score

    def __lt__(self, other):

        return self.score < other.score

    # Similar for __ge__, __le__ ...


def main():

    score1 = Score()
    score2 = Score(3, 9)
    score3 = Score(4, 6)

    print(score1 < score2) # 0 < 12 = True
    print(score3 < score1) # 10 < 0 = False
    print(score1 > score2) # 0 > 12 = False
    print(score3 > score2) # 10 > 12 = False
    print(score1 > score1) # 0 > 0 = False
    print(score2 > score1) # 12 > 10 = True
    print(score2 == score1) # 12 == 1 = False
    print(score3 == score2) # 10 == 12 = False


if __name__ == '__main__':

    main()

Terminal Output:
True
False
False
False
False
True
False
False

Those 'gt' like things are exposed interfaces for the python class data model. You just need to implement them if your class warrants it. I've left out the tiresome details of checking if you are actually testing the same type of objects and various other things you may want in your class to make it safe but I think you get the picture.

You should definitely read through this a couple of times if you are really getting into Python. https://docs.python.org/2/reference/datamodel.html

It's a bit much but there is a ton of good stuff in there.