"Enable" comparisons for classes

I need help at CSC HW. This is on classes / objects, and this is a simple class for defining a circle with a class name Circle (object).

The exact text is HW (I completed the first two parts of this hw, and thus this 3rd part is an extension of the original task):

"" Expand the Circle class by enabling the comparison of Circle objects using operators such as <,>,> =, <=, == and and = =, where one circle is considered "larger" than the other if it is actually larger (i.e. has a large area) another 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 to display the coordinates and radius of a 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 in (3, 5). Radius: 4: 000000"

We pointed to a specific page of our textbook with mathematical operators for classes: eq (), gt (), ge (), lt (), le (), ne (), etc. So I thought my professor wanted something like that?

 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 need to create a definition / attribute attribute for each method that we want to use in the class? Thanks in advance.

+9
source share
4 answers

Define or override comparison operators for a class. http://docs.python.org/reference/expressions.html#notin

It seems that you are on the right track, except that you need to pass the second circle object for comparison. self refers to the first object of the circle. So self.r will give you r of the first circle. You also need to return True or False from the method.

 def __gt__(self, circle2): return self.r > circle2.r 

Note that this is just a comparison of r circles.

+15
source

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

Given a class that defines one or more ordering methods for comparative comparisons, this class decorator provides the rest. This simplifies the effort to identify all possible advanced comparison operations:

The class must define one of __lt__() , __le__() , __gt__() or __ge__() . In addition, the class must provide __eq__() .

+23
source

This is not really the answer to your question, but note that your calcArea(...) method, like not, really should not be inside the class. (Note that there is no reason to use self at all in it!)

Actually what you probably want is a method that is still inside the class, something like

 def area(self): return math.pi*(self.r**2) 

so that it actually uses the radius of the circle you are in.

+1
source

Short answer: Yes. You must define the comparison operators that you want to support if you do not inherit this behavior from the base class.

0
source

Source: https://habr.com/ru/post/886918/


All Articles