如何在类中添加@class方法,以允许使用元组而不是中心位置的点来创建圆形对象

时间:2018-08-19 20:44:39

标签: 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):
        return cls(center, radius)

    @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)

我在Circle类中添加了一个名为from_tuple()的@class方法,该方法允许使用元组而不是Point来创建Circle对象作为中心位置。与其他任何Circle对象一样,生成的实例应以Point为中心。我应该对它进行编码,因此需要元组输入,但是允许半径默认为1。预期的输出如下:

center_point = 3, 4
circle = Circle.from_tuple(center=center_point)
print(circle)
    Circle(center=Point(3, 4), radius=1)
circle = Circle.from_tuple(center=center_point, radius = 3) 
print(circle)
    Circle(center=Point(3, 4), radius=3)
circle = Circle.from_tuple(center_point, 2)
print(circle)
    Circle(center=Point(3, 4), radius=2)
circle = Circle.from_tuple()
    Traceback (most recent call last):
    [...]
    TypeError: from_tuple() missing 1 required positional argument: 
    'center'

我写了我的类方法,但是它总是导致错误TypeError:from_tuple()缺少1个必需的位置参数:“ radius”。我找不到解决方法。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

最简单的方法是从该元组创建一个Point,然后通过__init__将其移交给cls

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

在这里,我还给了radius一个默认值,因此可以像您的__init__方法一样,仅用其中心坐标来描述一个半径为1的圆