[英]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.