Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Enabling" comparison for classes [duplicate]

Tags:

I would like some help on my CSC HW. It is on classes/objects, and it's a simple class on defining a circle, with the name class Circle(object).

The exact text of the HW (I completed the first two parts of this hw and thus this 3rd part is an expansion on the initial problem):

"""Expand on your Circle class by enabling the comparison of Circle objects using operators such as <, >, >=, <=, ==, and !=, where one Circle is considered "larger" than another if it is in fact larger (i.e., has greater area) the other Circle.

The following code:

A = Circle(2, 5, 1.5) B = Circle(-6, 1, 1) print A < B, A != B, A >= B 

Should generate this output:

False True True 

This is my code for displaying the coordinates and radius of the circle:

class Circle(object):     def __init__(self, x=0, y=0, r=0):         self.x = x         self.y = y         self.r = r     def __str__(self):         return "Circle at (%d , %d). Radius: %f" % (self.x, self.y, self.r)  def main():     print Circle(3, 5, 4.0)  main() 

The output of this class is "Circle at (3 , 5). Radius: 4:000000"

We were pointed to a certain page of our textbook with math operators for classes: eq(), gt(), ge(), lt(), le(), ne(), etc. So I was thinking, did my professor want something like this?

import math class Circle(object):     def __init__(self, x=0, y=0, r=0):         self.x = x         self.y = y         self.r = r     def __str__(self):         return "Circle at (%d , %d). Radius: %f" % (self.x, self.y, self.r)     def calcArea(self, r):         self.r = r         return (math.pi)*(r**2)     def __gt__(self, circ1Radius, circ2Radius)         self.circ1Radius = circ1Radius         self.circ2Radius = circ2Radius         r1 = circ1Radius         r2 = circ2Radius         r1 > r2 or r2 > r1     def __ge__(self, circ1Radius, circ2Radius)     #And so on for __lt__(), __le__(), __ne__(), etc def main():     A = Circle(3,4,1.5)      B = Circle(1,2,5.0)     C = Circle(5,7,7)      D = Circle(9,8,3)     print A < B, B > C, A < C, A >= C main() #Output should be "True, False, True, False" 

Do we have to make a definition/attribute for each method we want to use in the class? Thank you in advance.

like image 670
Derrick Avatar asked Apr 28 '11 20:04

Derrick


1 Answers

You can use the total_ordering decorator from functools, which generates all the missing compare methods if you supply __eq__() and one other.

Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. This simplifies the effort involved in specifying all of the possible rich comparison operations:

The class must define one of __lt__(), __le__(), __gt__(), or __ge__(). In addition, the class should supply an __eq__() method.

For Example,

@total_ordering class Student:     def _is_valid_operand(self, other):         return (hasattr(other, "lastname") and                 hasattr(other, "firstname"))     def __eq__(self, other):         if not self._is_valid_operand(other):             return NotImplemented         return ((self.lastname.lower(), self.firstname.lower()) ==                 (other.lastname.lower(), other.firstname.lower()))     def __lt__(self, other):         if not self._is_valid_operand(other):             return NotImplemented         return ((self.lastname.lower(), self.firstname.lower()) <                 (other.lastname.lower(), other.firstname.lower())) 
like image 132
Juri Robl Avatar answered Sep 28 '22 08:09

Juri Robl