globalList = []
class MyList:
def __init__(self):
self.myList = [1, 2, 3]
@property
def myList(self):
return self.myList.extend(globalList)
@myList.setter
def myList(self):
return self.myList
mL1 = MyList()
print("myList: ", mL1.myList)
mL1.myList.append(4)
print("after appending a 4, myList: ", mL1.myList)
我的目标是能够调用评估 myList 实例属性和全局对象 globalList 的 mL1.myList,但这是错误:
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\Desktop\Dropbox\sandbox.py", line 14, in <module>
mL1 = MyList()
File "C:\Documents and Settings\Administrator\Desktop\Dropbox\sandbox.py", line 6, in __init__
self.myList = [1, 2, 3]
TypeError: myList() takes 1 positional argument but 2 were given
我应该怎么做才能解决这个问题?