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)
Result:
myList: [1, 2, 3]
after appending a 4, myList: [1, 2, 3]
after extend, myList: [1, 2, 3]
The problem I am facing is that mL1.myList.append(4) and mL1.myList.extend([5,6,"eight","IX"]) do not modify the _myList attribute in the mL1 object. How could I do to resolve the problem?
append()add to_myList(meaning the values won't be added to the end as one would expect) or toglobalList? Either way, it's pretty clear this wouldn't work as the returned value is a new list produced by concatenating two other lists. Extending that won't somehow modify back. - Gareth LattyMyListact as a sequence itself by implementing__getitem__(),append(), etc... on it directly, and it would also make it easier to implement. - Gareth Latty