我的LabelledPoint类继承自Point类。我希望此类具有x和y坐标以及将是字符串的label属性。
import numbers
def clamp(v,_min,_max):
return max(min(v,_max),_min)
class Point:
def __init__(self,x,y):
self.x = clamp(x,0,10)
self.y = clamp(y,0,10)
def __str__(self):
return(str((self.x,self.y)))
@property
def _add_(self,p):
a=self.x + p.x
b=self.y + p.y
return Point(a,b)
@property
def x(self):
return self.__x
@x.setter
def x(self, value):
if not isinstance(value, numbers.Number):
raise AttributeError()
self.__x = clamp(int(value), 0, 10)
@property
def y(self):
return self.__y
@y.setter
def y(self, value):
if not isinstance(value, numbers.Number):
raise AttributeError()
self.__y = clamp(int(value), 0, 10)
class LabelledPoint(Point):
def __init__(self,x,y,label):
Point.__init__(self,x,y)
label=str()
self.label=label
def __str__(self):
return (str((self.label)))
def _get_label(self):
return self._label
def _set_label(self,label):
raise AttributeError()
label=property(_get_label,_set_label)
return label
我想获得此输入:
lp = LabelledPoint(5,15, "foo")
print (lp) # output "(5,10) : foo"
我仅获得输出:
(5,10)
我不知道我的错误在哪里 谢谢
答案 0 :(得分:1)
_str_
您的
做任何事情Point.__str__(self)
方法周围只有一个下划线。它也不会对值dd$time = Map(f = seq, dd$start, dd$end) tidyr::unnest(dd[c("val", "time")]) # val time # 1 a 2 # 2 a 3 # 3 a 4 # 4 b 2 # 5 b 3 # 6 b 4 # 7 b 5 # 8 c 1 # 9 c 2 # 10 c 3
(我自己不能说更好)
答案 1 :(得分:1)
您在代码中遇到了很多不幸(请参阅注释):
import numbers
def clamp(v, _min, _max):
return max(min(v, _max), _min)
class Point:
def __init__(self, x, y):
self._x = clamp(x, 0, 10) # changed from no underscore to single underscore
self._y = clamp(y, 0, 10)
def __str__(self): # added explicit return of the class name
return f"{self.__class__.__name__}{str((self._x, self._y))}"
def __add__(self, p): # changed from double underscore to single underscore, and removed the @property
a = self._x + p._x
b = self._y + p._y
return self.__class__(a, b) # use super() i/o explicit class
@property
def x(self):
return self._x
@x.setter
def x(self, value): # changed from double underscore to single underscore everywhere below this point
if not isinstance(value, numbers.Number):
raise AttributeError()
self._x = clamp(int(value), 0, 10)
@property
def y(self):
return self._y
@y.setter
def y(self, value):
if not isinstance(value, numbers.Number):
raise AttributeError()
self._y = clamp(int(value), 0, 10)
class LabelledPoint(Point):
def __init__(self, x, y, label):
super().__init__(x, y) # calling super() i/o explicit class
self._label = str(label) # assign label to attribute
def __str__(self): # call to super(), then concatenate the label value
return f"{super().__str__()}: {str(self._label)}"
def get_label(self): # remove single underscore from getter and setter
return self._label # why not use @property like in the superclass?
def set_label(self, label): # remove raise exception that made th code unreachable
self._label = label
if __name__ == '__main__':
p = Point(2, 12)
print(p)
lp = LabelledPoint(5, 15, "foo")
print(lp)
Point(2, 10)
LabelledPoint(5, 10): foo