I'm having trouble understanding how to properly set up a contains method in my class. I know it automatically uses the operator "in" when you call it, i just don't think I understand how to set it up correctly.
I have to use it to see if anotherCircle is contained within a specific circle (both input from the user). The prof had us do two different types of methods for this.
The first one I have no problems with and more or less understand what it is doing, It is as follows:
def contains(self, circle2d):
dist = math.sqrt((circle2d._x - self._x)**2 + (circle2d._y - self._y)**2) #Distance of second circle's coords from the first circle's coords
if dist + circle2d._radius <= self._radius:
return True
However, the next method, which is supposed to do the same thing, uses the contains method so that we can call it with in in the main function. All I have is this:
def __contains__(self, anotherCircle):
if anotherCircle in self:
return True
I get multiple errors when I try to run this. I think i'm missing something on self, but I'm not sure what? Could someone please try to explain to me what exactly you need to do when you're writing a contains method such as this?