Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的属性类转换_Python_Class_Oop_Attributes - Fatal编程技术网

python中的属性类转换

python中的属性类转换,python,class,oop,attributes,Python,Class,Oop,Attributes,LabelledPoint类继承自Point类。我希望这个类有x和y坐标以及一个标签属性,它将是一个字符串 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__(sel

LabelledPoint类继承自Point类。我希望这个类有x和y坐标以及一个标签属性,它将是一个字符串

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)
我不知道我的错误在哪里 谢谢

您的_str_u方法周围只有一个下划线。它对值点也没有任何作用

@帕特里克·豪
我自己说得再好不过了

您的代码中有不少错误请参见注释:

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)
输出:
您的_str_u方法周围只有一个下划线。我编辑了代码,我不知道为什么代码在运行,但从未编译过。我还添加了两个其他函数感谢您的注释解释!!
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