我有一个Point
接受位置、值和标志作为参数的类。这个类应该只接受整数作为位置和值参数。我尝试了下面的代码,但它不能正常工作。
class PointException(Exception):
pass
class Point():
def __init__(self, position, value, flag=False):
try:
if all([isinstance(x, int) for x in position, value]):
self.position = position
self.value = value
self.point = (position, value)
self.flag = flag
except:
raise PointException("Foo value and position must be integers.")
def __repr__(self):
return "< {0}, {1}, {2} >".format(self.position, self.value, self.flag)
def __eq__(self, other):
if not isinstance(other, Point):
return False
try:
return all([self.point == other.point, self.flag == other.flag])
except AttributeError:
return False
def __ne__(self, other):
return not self.__eq__(other)
更新
AttributError
例如,当我尝试时,我得到一个Point(1, 1.2)
。
AttributeError: Point instance has no attribute 'position'