目的是把一个函数变成一个只可读的属性进行读取操作,如果进行修改就会出错。
class Voc:
@property
def vocab_size(self):
return 4
t = Voc()
print(t.vocab_size)
t.vocab_size = 3
>>4
Traceback (most recent call last):
File "C:/Users/MapReducer/.PyCharmCE2018.1/config/scratches/scratch_1.py", line 12, in <module>
t.vocab_size = 3
AttributeError: can't set attribute
网友评论