Discuss / Python / 这节讲的有点含糊的样子

这节讲的有点含糊的样子

Topic source

南小i

#1 Created at ... [Delete] [Delete and Lock User]

为什么可以直接这么写

@birth.setter
def birth(self, value):
        self._birth = value

这个setter是啥呢,还是得看一下property的原理才能理解

南小i

#2 Created at ... [Delete] [Delete and Lock User]

作业

class Screen(object):
    @property
    def width(self):
        return self._width

    @width.setter
    def width(self, value):
        self._width = value

    @property
    def height(self):
        return self._height

    @width.setter
    def height(self, value):
        self._height = value

    @property
    def resolution(self):
        return self._height * self._width

实现倒是不难

发现这里@width.setter里的方法名字也叫width是为了方便使用,实际上最后测试里

print(s.width)

s.width = 1024

这两行的width实际上是两个东西了


  • 1

Reply