< __ main__.Circle对象位于0x05DE4250>

时间:2017-08-19 04:07:51

标签: python

有人可以帮忙吗?

我正在尝试让这个程序正常工作

<

结果应为

class Circle:
    def __init__(self, radius):
        self.__radius = radius;
        if self.__radius <= 0:
            raise ValueError('must not be less than or equal to 0')
        elif not isinstance(self.__radius, (int, float)):
            raise TypeError('must be an integer value')

def main():
    try:
        c = Circle("n")
    except ValueError as x:
        print("Error: " + str(x))
    else:
        print(c)

main()

但相反,我得到了:

Error: Radius must be an integer value

如果我改变了

的值
TypeError: '<=' not supported between instances of 'str' and 'int'

c = Circle("n")

我会得到答案,但如果我将其改为

c = Circle(-10)

我会收到此错误

c = Circle(10)

1 个答案:

答案 0 :(得分:0)

  

TypeError: '<=' not supported between instances of 'str' and 'int'

您需要更改支票的顺序,以便在进行比较之前首先检查类型是否正确。

class Circle:
    def __init__(self, radius):
        self.__radius = radius
        if not isinstance(self.__radius, (int, float)):
            raise TypeError('must be an integer value')
        if self.__radius <= 0:
            raise ValueError('must not be less than or equal to 0')

现在,关于这个:

  

<__main__.Circle object at 0x05DE4250>

这只是对象的默认表示形式,您可以通过覆盖类中的__repr__方法来自定义此对象:

def __repr__(self):
   return 'Circle of radius: {}'.self(radius)