所以我在Python 3与运营商超载使我的第一个程序,我stucked在添加(+)运算符。__add__方法在Python 3
def __add__(self, newMember):
if(isinstance(newMember, Queue)):
tempList=self.myQueue[:] # makes a copy
tempList.extend(newMember.myQueue)
return Queue(tempList)
def __str__(self):
if not self.myQueue:
string="."
else:
string=""
for x in self.myQueue:
string=string+str(x)
if(x<len(self.myQueue)):
string=string+", "
else:
string=string+"."
return string
基本上我正在做Queue类(I已经知道有这样一个存在),然后通过键入C = C1 + C2连接两个队列对象。但是当我打印(c)时,它弄乱了“,”和“。”。不能得到什么错误。 有什么帮助吗?
你不应该做'tempList =队列(self.myQueue)'或类似的东西?我不太了解Python,但是如果你只是将'self.myQueue'赋值给'tempList',那么'tempList'指的是与'self.myQueue'相同的对象... – dreamlax 2012-04-21 13:52:14
我编辑了我的问题。我真的对同一个对象有所反应,所以我添加了[:]。我不希望tempList是一个队列类型,但一个列表类型,所以我可以返回队列(列表) – Jjang 2012-04-21 14:04:01
为什么这标记为[C++]? – geoffspear 2012-04-21 14:07:27