Search code examples
pythonattributeerror

How to fix the AttributeError: 'str' object has no attribute '_radius'?


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?


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