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 为什么属性不能正常工作_Python - Fatal编程技术网

Python 为什么属性不能正常工作

Python 为什么属性不能正常工作,python,Python,我的程序有一个问题,即在输入中输入一个字母后,程序崩溃。任务要求我必须借助属性来控制数据是否正确。然而,好像程序根本就不去那里。我知道,对int进行强制转换时,它将始终通过属性并在计数时抛出,但当它不投影时,它将无论如何抛出程序。请帮忙。这是我的密码: import math class Point: def __init__(self, x, y): self.x = x self.y = y @property def x(se

我的程序有一个问题,即在输入中输入一个字母后,程序崩溃。任务要求我必须借助属性来控制数据是否正确。然而,好像程序根本就不去那里。我知道,对int进行强制转换时,它将始终通过属性并在计数时抛出,但当它不投影时,它将无论如何抛出程序。请帮忙。这是我的密码:

 import math


class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, x):

        if not isinstance(x, int):
            raise TypeError(f"{x} is not INT")
        self._x = x

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, y):
        if not isinstance(y, int):
            raise TypeError(f"{y} is not INT")
        self._y = y

    def __str__(self):
        return "coordinate x: {0.x}\tcoordinate y: {0.y}".format(self)

    def __repr__(self):
        return f"{self.__class__.__name__}(x:{self.x},y:{self.y})"

    def __eq__(self, other):
        if self.__class__.__name__ == other.__class__.__name__:
            return self.x == other.x and self.y == other.y
        raise TypeError("Is not object of class Point")

    def distance_from_origin(self, x, y):
        odl = math.sqrt((0 - x) ** 2 + (0 - y) ** 2)
        return odl


class Circle(Point):
    def __init__(self, x, y, radius):
        super().__init__(x, y)
        self.radius = radius

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, radius):
        if radius < 0:
            raise ValueError('radius must be > 0')
        self._radius = radius

    def __repr__(self):
        return f"{self.__class__.__name__}(x:{self.x},y:{self.y},promień:{self.radius})"

    def __str__(self):
        return "coordinate circle x: {0.x}\tcoordinate circle y: {0.y}\tcircle radius: {0.radius}".format(self)

    def area(self):
        pole = (math.pi * (self.radius ** 2))
        return pole

    def circumference(self):
        obw = 2 * math.pi * self.radius
        return obw

    def edge_distance_from_origin(self):
        odl = math.sqrt((0 - self.x) ** 2 + (0 - self.y) ** 2)
        brzeg = odl - self.radius
        return brzeg

    def __eq__(self, other):
        super().__eq__(other)
        if self.__class__.__name__ == other.__class__.__name__:
            return self.radius == other.radius
        raise TypeError("is not object of class Circle")


if __name__ == '__main__':
    x = int(input("enter the coordinate x: "))
    y = int(input("enter the coordinate y: "))
    p1 = Point(x, y)
    p2 = Point(5, 7)

    c1 = Circle(5, 5, 5)
    c2 = Circle(3, 3, 5)
    print(p1)  # str
    print(repr(p1))  # repr
    print(c1)
    print(repr(c1))
    print("Distance from the center of the coordinate axes:", round(p1.distance_from_origin(x, y), 2))
    print(p1 == p2)
    print(round(c1.area(), 2))
    print("Distance of the edge of the circle from the center of the coordinate axis:", round(c1.edge_distance_from_origin(), 2))
    print(c1 == c2)

因为属性具有相同的名称。不,它们没有,并且不在名称中是问题。问题在于输入..看,您有一个属性
x
。它是一个类属性,意味着它附加到类。现在,如果执行
self.x=…
,将创建am实例属性。它将对类上的属性进行阴影处理。这一行中有一个错误:
print(“与坐标轴中心的距离:”,round(p1.与原点的距离(x,y),2))
:x和y为undefined@Klaus我需要更改atributes类的名称???
def __init__(self, x, y):
        self._x = x
        self._y = y

    @property
    def x(self):
        return self.x

    @x.setter
    def x(self, x):

        if not isinstance(x, int):
            raise TypeError(f"{x} is not INT")
        self.x = x

    @property
    def y(self):
        return self.y

    @y.setter
    def y(self, y):
        if not isinstance(y, int):
            raise TypeError(f"{y} is not INT")
        self.y = y