-1
所以我有一个点类,我声明一个点,然后对其执行操作。其中一个操作是缩放,如果该点不是浮点值,则会引入一个点并缩放它,同时产生错误。这里是什么样子:Python - 点类不返回正确的点
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
所以错误信息e看起来不错,但它打印的值不是。那么为什么不打印出正确的值呢?这里是我的初始化和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)))
我想'scale'是'Point'类的一部分。一个显而易见的错误是在'scale'方法中使用'self.x0'和'self.y0'(应该是'x'和'y'),因为'__str__'方法使用'x'和'y'。 – shahkalpesh