Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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_Python 3.x - Fatal编程技术网

Python 添加一个允许圆心类';要使用元组(而不是点)作为输入修改的实例

Python 添加一个允许圆心类';要使用元组(而不是点)作为输入修改的实例,python,python-3.x,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

我编写了以下代码,其中包含一个点和圆类:

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)
导入数学
课程点:
“二维点(x,y)”
定义初始化(self,x=0,y=0):
#初始化点实例
self.x=x
self.y=y
定义(自我):
屈服自我
屈服于自我
定义(自我、其他):
self.x=self.x+其他.x
self.y=self.y+其他.y
回归自我
定义添加(自身、其他):
返回点(self.x+other.x,self.y+other.y)
定义多个(自身、其他):
mulx=自身x*其他
muly=self.y*other
返回点(mulx、muly)
定义(自身、其他):
mulx=自身x*其他
muly=self.y*other
返回点(mulx、muly)
@类方法
来自参数组(cls,参数组)的def:
x、 y=tup
返回cls(x,y)
def loc_from_tuple(self,tup):
#self.x=t[0]
#self.y=t[1]
self.x,self.y=tup
@财产
def幅值(自):
#“”“将向量的大小从(0,0)返回到self。”“”
返回math.sqrt(self.x**2+self.y**2)
def距离(自身、自身2):
返回math.sqrt((self2.x-self.x)**2+(self2.y-self.y)**2)
定义(自我):
返回'pointat({},{})'.format(self.x,self.y)
定义报告(自我):
返回“点(x={},y={})”.format(self.x,self.y)
类圆():
“”“圆心为点实例的圆(圆心、半径)”
定义初始(自,中心=点(0,0),半径=1):
#点。初始点(自身,中心)
self.center=1*center
自半径=半径
#如果不存在(中心、点):
#raise TYPE ERROR(“中心必须是点!”)
@财产
def中心(自我):
返回自我中心
@中锋二传手
def中心(自身,_中心):
自我中心=\u中心
如果(isinstance(自身中心点)为False):
raise TYPE ERROR(“中心必须是点!”)
定义获取项目(自身,项目):
返回自我中心[项目]
定义(自我、其他):
self.center.x=self.center.x+其他.center.x
self.center.y=self.center.y+other.center.y
自半径=自半径+其他半径
self=圆(点(self.center.x,self.center.y),self.radius)
回归自我
定义添加(自身、其他):
返回圈(
点(self.center.x+other.center.x,self.center.y+other.center.y),
自半径+其他半径)
@类方法
来自组的def(cls,中心,半径=1):
返回cls(点(*中心),半径)
def center_from_tuple(self)
@财产
def半径(自身):
返回自我半径
@半径设定器
def半径(自身,半径):
如果半径小于0:
raise VALUERROR('半径不能为负')
自半径=半径
@财产
def区域(自身):
“”“计算并返回圆的面积”“”
返回math.pi*self.radius**2
@财产
def直径(自身):
“”“计算并返回圆的直径”“”
返回自半径*2
定义(自我):
返回“圆心位于({0},{1})且半径为{2}的圆”。格式(self.center.x,self.center.y,self.radius)
定义报告(自我):
返回“圆(圆心=点({0},{1}),半径={2})”格式(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'
circle=圆(点(2,3,2)
打印(圆圈)
圆(中心=点(2,3),半径=2)
新中心=4,5
圆。中心从\u元组(新的\u中心)
打印(圆圈)
圆(中心=点(4,5),半径=2)
圆。从_元组()以_为中心
回溯(最近一次调用):文件“”,第1行,在
TypeError:center\u from\u tuple()缺少1个必需的位置参数:
“中心”

我不知道该怎么做。任何想法都会有帮助。

您希望函数在作为参数传递单个元组时工作,但在没有参数的情况下调用时引发
TypeError:center\u from\u tuple()缺少1个必需的位置参数:“center”
。因此,很明显,它需要取一个名为
center
的参数(超出
self
),没有默认值:

def center_from_tuple(self, center):
现在,你想用这个元组做什么?将其存储为您的中心:

    self.center = …?
但是您的
中心
必须是
,而不是元组。那么,如何转换它呢?您已经为
编写了另一个构造函数
classmethod
,允许您将元组转换为
,因此只需调用它即可

    … Point.from_tuple(center)

那么我会设置self.center=Point.from_tuple(center)吗?我有点困惑。考虑到你的问题,我发现很难相信你写了这段代码。比如说,如果这是一个家庭作业,他们给了你一个骨架,让你完成缺失的部分,那很好,但隐瞒事实并不好。如果我们希望写出一个你可以使用的答案,我们需要知道你的理解程度。