The code is:

- Code: Select all
`import math`

class circle2d:

def __init__(self, x = 0, y = 0, radius = 0):

self.__x = x

self.__y = y

self.__radius = radius

def getx(self):

return self.__x

def gety(self):

return self.__y

def getradius(self):

return self.__radius

def setx(self, x):

self.__x = x

def sety(self, y):

self.__y = y

def setradius(self, radius):

self.__radius = radius

def getarea(self):

return self.__radius ** 2 * math.pi

def getcircumference(self):

return self.__radius * 2 * math.pi

def containspoint(self, x, y):

d = distance(x, y, self.__x, self.__y)

return d <= self.__radius

def contains(self, circle):

d = distance(self.__x, self.__y, circle.__x, circle.__y)

return d + circle.__radius <= circle.__radius

def overlaps(self, circle):

return distance(self.__x, self.__y, circle.__x, circle.__y) \

<= self.__radius + circle.__radius

def __contains__(self, anothercircle):

return self.contains(anothercircle)

def __lt__(self, secondcircle):

return self.__cmp__(secondcircle) < 0

def __le__(self, secondcircle):

return self.__cmp__(secondcircle) <= 0

def __gt__(self, secondcircle):

return self.__cmp__(secondcircle) > 0

def __ge__(self, secondcircle):

return self.__cmp__(secondcircle) >= 0

def __cmp__(self, secondcircle):

if self.__radius > secondcircle.__radius:

return 1

elif self.__radius < secondcircle.__radius:

return -1

else:

return 0

def distance(x1, y1, x2, y2):

return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

I understand it all except when it starts using circle, anothercircle, circle.__x, secondcircle, etc.

The code doesn't define a circle function or class, so where does circle.__x come from? Where is anothercircle and secondcircle defined?

Lastly, why are the comparison methods needed? if I create two objects from the class, I can't use the standard comparison operators without defining all of them?