如何修复AttributeError:'str'对象没有属性'_radius'?

时间:2014-02-09 07:41:10

标签: python attributeerror

我有这个python代码看起来很直接,但是当我尝试加载它时,我得到一个错误,如上所述。您也可以查看下面的完整错误消息。请问我做错了什么?谢谢你。

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

当我运行它并经过几个步骤后,shell只显示:

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'

我该如何修复代码?

3 个答案:

答案 0 :(得分:3)

Circle2D.__eq__假设anotherCircleCircle2D个实例。但是你传递了str对象。

要处理这个问题,您需要检查实例类型。

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

答案 1 :(得分:1)

您无法将Circle2D实例与字符串进行比较,因为__eq__(相等)方法在第二个对象上查找_radius属性,在这种情况下是一个字符串,因此没有。

要么你没有意识到c1Circle2D,要么你试图将它的等式与"Hello"进行比较作为早期测试,而不是期望它是真的。< / p>

如果您将c1与另一个Circle2D进行比较,则不会出现此错误。也许c2


当您尝试评估c1 == "Hello"时,Python实际上会调用此方法:c1.__eq__("Hello")

答案 2 :(得分:0)

假设c1是一个圆圈,那么表达式c1 == "Hello"会调用c1.__eq__("Hello"),在此函数中为anotherCircle指定“Hello”。 “Hello”是str