globalList = []
class MyList:
def __init__(self):
self._myList = [1, 2, 3]
@property
def myList(self):
return self._myList + globalList
@myList.setter
def myList(self, val):
self._myList = val
mL1 = MyList()
print("myList: ", mL1.myList)
mL1.myList.append(4)
print("after appending a 4, myList: ", mL1.myList)
mL1.myList.extend([5,6,"eight","IX"])
print("after extend, myList: ", mL1.myList)
结果:
myList: [1, 2, 3]
after appending a 4, myList: [1, 2, 3]
after extend, myList: [1, 2, 3]
我面临的问题是 mL1.myList.append(4) 和 mL1.myList.extend([5,6,"eight","IX"]) 不会修改 mL1 对象中的 _myList 属性。我该怎么做才能解决问题?