python实例属性没有init_Python“实例没有属性…”

我对Python有一些问题,所以我的类到此为止:class Rectangle:

def __init__(self, x1=0, y1=0, x2=0, y2=0):

if(x1 > x2):

raise ValueError("x1 cannot be bigger than x2!")

if(y1 > y2):

raise ValueError("y1 cannot be bigger than y2!")

self.pt1 = Point(x1, y1)

self.pt2 = Point(x2, y2)

def __str__(self):

return str("[(" + str(self.pt1.x) + ", " + str(self.pt1.y) + "), (" + str(self.pt2.x) + ", " + str(self.pt2.y) + ")]")

def __repr__(self):

return str("Rectangle(" + str(self.pt1.x) + ", " + str(self.pt1.y) + ", " + str(self.pt2.x) + ", "+ str(self.pt2.y) + ")")

def __eq__(self, other):

return (self.pt1== other.pt1 and self.pt2 == other.pt2)

def __ne__(self, other):

return not self == other

def center(self):

return Point((self.pt2.x - self.pt1.x) / 2, (self.pt2.y - self.pt1.y) / 2)

当我尝试在另一个类中使用方法“center”时:

^{pr2}$

我收到这样的信息:Rectangle instance has no attribute "center".

我不知道现在该怎么办,为什么不看我的方法?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值