Question

I have this python code which seem very straight forward but when I try to load it I get an error as above. you can view the full error message below too. Please what am I doing wrong? Thanks you.

import math

class Circle2D(object):
    def __init__(self, x = 0, y = 0, radius = 0):
        self._x = x
        self._y = y
        self._radius = radius
    def __str__(self):
        return "Circle with center (" + str(self._x) + ", " + str(self._y) + ")"
    def getX(self):
        return self._x
    def getY(self):
        return self._y
    def getArea(self):
        return (math.pi * self._radius**2)
    def getPerimeter(self):
        return (math.pi * 2 *self._radius)
    def containsPoint(self, x, y):
        if (((x - self._x)**2 - (y - self._y)**2) < self._radius**2):
            return True
        else:
            return False
    def contains(self, second):
        distance = math.sqrt((self._x - second._x)**2 + (self._y - second._y)**2)
        if ((second._radius + distance) <= self._radius):
            return True
        else:
            return False
    def overlaps(self, second):
        distance = math.sqrt((self._y - second._y)**2 + (self._x - second._x)**2)
        if (distance <= (self._radius + second
                         ._radius)):
            return True
        else:
            return False
    def __contains__(self, anotherCircle):
        distance = math.sqrt((self._x - anotherCircle._x)**2 + (self._y - anotherCircle._y)**2)
        if(self._radius >= (anotherCircle._radius + distance)):
            return True
        else:
            return False
    def __cmp__(self, anotherCircle):
        if self._radius > anotherCircle._radius:
            return 1
        elif self._radius > anotherCircle._radius:
            return -1
        else:
            return 0
    def __eq__(self, anotherCircle):
       if self._radius == anotherCircle._radius:
           return True
       else:
            return False
    def __ne__(self, anotherCircle):
        if self._radius == anotherCircle._radius:
           return False
        else:
            return True

when i runn it and after few steps, the shell just shows:

Traceback (most recent call last):
  File "C:\Users\wxwdd_000\Desktop\HW_2.py", line 124, in <module>
    main()
  File "C:\Users\wxwdd_000\Desktop\HW_2.py", line 121, in main
    print 'c1 == "Hello"?', c1 == "Hello"
  File "C:\Users\wxwdd_000\Desktop\HW_2.py", line 57, in __eq__
    if self._radius == anotherCircle._radius:
AttributeError: 'str' object has no attribute '_radius'

how can i fix the code?

Was it helpful?

Solution

Circle2D.__eq__ assumes anotherCircle is Circle2D instance. But you're passing str objecct.

To handle that, you need to check instance type.

def __eq__(self, anotherCircle):
   return isinstance(anotherCircle, Circle2D) and \
          self._radius == anotherCircle._radius

OTHER TIPS

You cannot compare a Circle2D instance against a string, because the __eq__ (equality) method looks for a _radius attribute on the second object, which in this case is a string and therefore has none.

Either you didn't realize c1 was a Circle2D, or you are trying to compare its equality to "Hello" as an early test with no expectation for it to be True.

You won't get this error if you compare c1 against another Circle2D. Maybe c2?


When you're trying to evaluate c1 == "Hello", Python actually calls this method: c1.__eq__("Hello")

Supposing that c1 is a circle, then the expression c1 == "Hello" calls c1.__eq__("Hello"), assigning "Hello" to anotherCircle inside this function. And "Hello" is a str.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top