簡體   English   中英

如何設置中心和半徑的默認值Point(0,0)= 1?

[英]How do I set the default value Point(0,0) for center and radius = 1?

請幫我修改Circle類,(1)設置一個默認實例。

圓類使用Point對象為中心。 使用center和radius關鍵字,將center作為第一個參數。 使用reprstr 將點對象和半徑值分配給圓時, 分配值的輸出應如下所示

這就是我得到的

沒有輸入的圓的默認值為:以Point對象的默認實例為中心,半徑為1。

這是默認值的輸出結果

這就是我要獲得的默認值

如果我要更改以下內容

def __repr__(self):
     return "Circle(center=Point({0}, {1}), 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)

這將產生一個錯誤,因為分配給中心Point(2,3)的點對象不支持索引。

 import math
class Point:
    def __init__(self,x=0,y=0):
        self.x = x
        self.y = y  
    def __iter__(self):
        yield self.x
        yield self.y    
    def __add__(self,other):
        return Point(self.x+other.x,self.y+other.y) 
    def __mul__(self,n):
        return Point(self.x*n,self.y*n) 
    def __rmul__(self,n):
        return Point(n*self.x,n*self.y) 
    @classmethod
    def from_tuple (cls, self=(0,0)):
        return cls(*self)   
    def loc_from_tuple(self,t=(0,0)):
        self.x=t[0]
        self.y=t[1] 
    def __str__(self):
        return "Point at ({0}, {1})".format(self.x,self.y)  
    def __repr__(self):
        return"Point(x={0}, y={1})".format(self.x,self.y)   
    @property
    def magnitude(self):
        return math.sqrt(self.x**2+self.y**2)   
    def distance (self, other):
        return math.sqrt((self.x-other.x)**2+(self.y-other.y)**2)       
    def shift(p1, p2):
        mx = p1.x+p2.x
        my = p1.y+p2.y
        return Point(mx,my) 
class Circle(Point):
    def __init__(self, center=(0,0), radius=1):
        Point.__init__(self,center)
        self.center=center
        self.radius=radius
    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.x,self.center.y,self.radius)
    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 diameter(self):
        return self._radius*2
    @diameter.setter
    def diameter(self, diameter):
        if diameter<0:
            raise ValueError('The radius cannot be negative')
        self._radius = diameter/2
    @property
    def area(self):
        return math.pi*self.radius**2

Center是一個元組,因此它沒有屬性x 在這里定義中心:

class Circle(Point):
    def __init__(self, center=(0,0), radius=1):
        Point.__init__(self,center)
        self.center=center

在這里,您致電center.x

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

一個快速的解決方法是將__repr__更改為以下內容:

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

這樣,您將為元組建立索引。 另一種方法是使center成為字典,例如center = {'x':0, 'y'0} 然后__repr____repr__工作,盡管您必須遍歷程序的其余部分以確保一致性。

錯誤消息告訴您您需要了解的所有信息:AttributeError:'tuple'對象沒有屬性'x'

令人討厭的一句話是:

return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.center.x,self.center.y,self.radius)

對象center是一個元組,這意味着其變量是通過索引而不是屬性名稱訪問的。 請嘗試:

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

更好的方法是實際利用CirclePoint繼承的繼承關系並直接訪問xy如果已經有了center ,則沒有理由將center保留為實例變量:

return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.x,self.y,self.radius)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM