Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python <__0x05DE4250处的主圆圈对象>;_Python - Fatal编程技术网

Python <__0x05DE4250处的主圆圈对象>;

Python <__0x05DE4250处的主圆圈对象>;,python,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, f

有人能帮忙吗

我正在努力使这个计划奏效

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()
但是,相反,我得到的是:

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

我会得到答案,但如果我改成

c = Circle(10)
我会得到这个错误

<__main__.Circle object at 0x05DE4250>


TypeError:“最后一行不是错误:它是
print
函数的输出。第一行是错误,试图比较
int
str
。第二个不是错误,这是类的
str
表示。您没有告诉Python如何打印
Circle
对象,因此它会打印它的一些通用形式;这就是您所看到的。您的异常只检查
ValueError
,而在您的情况下,您的异常是
TypeError
。因此(在本例中),
TypeError
也应该作为
main
中的可能异常。
c = Circle(-10)
c = Circle(10)
<__main__.Circle object at 0x05DE4250>
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')
def __repr__(self):
   return 'Circle of radius: {}'.self(radius)