Discuss / Python / 并不是很懂这个地方

并不是很懂这个地方

Topic source

XERIN24040

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

当用到@property时,可以直接s.score = 60;而没有用到@property时,需要s.set_score(60)。

但如果用到@时,输入s.score(60)就会拨错

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    s.score(60)
  File "<pyshell#1>", line 5, in score
    return self._score
AttributeError: 'Student' object has no attribute '_score'

有同学可以讲讲这是为什么嘛

但好像只要记住有@就赋值,没@就直接调用函数就行了

zhanglei74259

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

Python内置的@property装饰器就是负责把一个方法变成属性调用的 原话啊  就是为了更加简介而采用了@property的形式


  • 1

Reply