【发布时间】:2015-12-21 01:24:37
【问题描述】:
假设我们有以下代码:
class A:
def __init__(self):
self.x = [p for p in range(10)]
def __getitem__(self, key):
return self.x[key]
a = A()
print a[2] #will return a.x[2] = 2
现在如何定义a[i] 的设置器?我将如何定义装饰器?
简单属性的过程非常简单,但在这种情况下,Python 如何理解我想为a[i] 定义一个setter(类似于@a[i].setter)?
【问题讨论】:
标签: python decorator getter-setter python-decorators magic-methods