[英]Python - point class not returning correct points
因此,我有一個Point類,在其中聲明一個點,然后對其進行操作。 如果點不是浮點值,則操作之一就是縮放,該操作會獲取一個點並對其進行縮放,同時引發錯誤。 看起來是這樣的:
def scale(self, f):
if not isinstance(f, float):
raise Error("Parameter \"f\" illegal.")
self.x0 = f * self.x
self.y0 = f * self.y
如果我使用以下測試代碼進行測試:
print '*** scale'
# f illegal
try:
p0 = Point(0.0, 0.0)
p0.scale(1)
except Error as e:
print 'caught:', e.message
# normal case
p0 = Point(2.0, 3.0)
p0.scale(2.3)
print p0
然后我得到的輸出是:
*** scale
caught: Parameter "f" illegal.
2 3
但是我想要的輸出是:
*** scale
caught: Parameter "f" illegal.
5 7
因此,錯誤消息看起來不錯,但打印出來的值卻不正確。 那為什么不打印出正確的值呢? 這是我的init和str方法:
def __init__(self, x, y):
if not isinstance(x, float):
raise Error("Parameter \"x\" illegal.")
self.x = x
if not isinstance(y, float):
raise Error ("Parameter \"y\" illegal.")
self.y = y
def __str__(self):
return '%d %d' % (int(round(self.x)), int(round(self.y)))
您正在分配新屬性 :
self.x0 = f * self.x
self.y0 = f * self.y
x0
和y0
是與x
和y
不同的屬性。 因此, x
和y
保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.