添加一种方法,该方法允许使用元组(而不是点)作为输入来修改圆类的实例的中心

时间:2018-08-19 23:01:46

标签: python python-3.x

我编写了以下包含点和圆的类的代码:

import math
class Point:
    """Two-Dimensional Point(x, y)"""
    def __init__(self, x=0, y=0):
        # Initialize the Point instance
        self.x = x
        self.y = y

    def __iter__(self):
         yield self.x
         yield self.y

    def __iadd__(self, other):
         self.x = self.x + other.x
         self.y = self.y + other.y
         return self

    def __add__(self, other):
         return Point(self.x + other.x, self.y + other.y)

    def __mul__(self, other):
        mulx = self.x * other
        muly = self.y * other
        return Point(mulx, muly)

    def __rmul__(self, other):
        mulx = self.x * other
        muly = self.y * other
        return Point(mulx, muly)

    @classmethod
    def from_tuple(cls, tup):
        x, y = tup
        return cls(x, y)

    def loc_from_tuple(self, tup):
  #       self.x=t[0]
  #       self.y=t[1]
        self.x, self.y = tup

    @property
    def magnitude(self):
        # """Return the magnitude of vector from (0,0) to self."""
        return math.sqrt(self.x ** 2 + self.y ** 2)

    def distance(self, self2):
         return math.sqrt((self2.x - self.x) ** 2 + (self2.y - self.y) ** 2)

    def __str__(self):
        return 'Point at ({}, {})'.format(self.x, self.y)

    def __repr__(self):
        return "Point(x={},y={})".format(self.x, self.y)

class Circle():
    """Circle(center, radius) where center is a Point instance"""
    def __init__(self, center= Point(0,0), radius=1):
 #       Point.__init__(self,center)
        self.center = 1 * center
        self.radius = radius
 #       if not isinstance(center,Point):
 #           raise TypeError("The center must be a Point!")

    @property
    def center(self):
        return self._center

    @center.setter
    def center(self, _center):
        self._center = _center
        if (isinstance(self._center, Point) is False):
            raise TypeError("The center must be a Point!")

    def __getitem__(self,item):
        return self.center[item]


    def __iadd__(self, other):
        self.center.x = self.center.x + other.center.x
        self.center.y = self.center.y + other.center.y
        self.radius = self.radius + other.radius
        self = Circle(Point(self.center.x, self.center.y), self.radius)
        return self

    def __add__(self,other):
        return Circle(
        Point(self.center.x + other.center.x, self.center.y+other.center.y),
        self.radius + other.radius)

    @classmethod
    def from_tuple(cls, center,radius = 1):
        return cls(Point(*center), radius)

    def center_from_tuple(self)


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

    @radius.setter
    def radius(self, radius):
         if radius < 0:
             raise ValueError('The radius cannot be negative')
         self._radius = radius

    @property
    def area(self):
        """Calculate and return the area of the Circle"""
        return math.pi * self.radius ** 2

    @property
    def diameter(self):
        """Calculate and return the diameter of the Circle"""
        return self.radius * 2

    def __str__(self):
        return "Circle with center at ({0}, {1}) and radius {2}".format(self.center.x, self.center.y, self.radius)

    def __repr__(self):
        return "Circle(center=Point({0}, {1}), radius={2})".format(self.center[0],self.center[1],self.radius)

我想添加一个center_from_tuple()方法,该方法允许使用元组(而不是点)作为输入来修改圆实例的中心。我想对其进行编码,因此需要元组输入,但是它不允许修改半径。例如:

circle = Circle(Point(2, 3), 2)
print(circle)
    Circle(center=Point(2, 3), radius=2)
new_center = 4, 5
circle.center_from_tuple(new_center)
print(circle)
    Circle(center=Point(4, 5), radius=2)
circle.center_from_tuple()
    Traceback (most recent call last): File "<stdin>", line 1, in <module>
    TypeError: center_from_tuple() missing 1 required positional argument: 
    'center'

我不能全神贯注于如何做到这一点。任何想法都会有所帮助。

1 个答案:

答案 0 :(得分:0)

您希望函数在传递单个元组作为参数时工作,但在不带参数调用时提高TypeError: center_from_tuple() missing 1 required positional argument: 'center'。因此,显然,它需要采用一个名为self的参数(超出center),而没有默认值:

def center_from_tuple(self, center):

现在,您想对该元组做什么?将其存储为您的中心:

    self.center = …?

但是您的center必须是Point,而不是元组。那么,如何转换呢?您已经为classmethod写了一个备用构造函数Point,可以将元组变成Point,所以只需调用它即可。

    … Point.from_tuple(center)