python中iadd与add_在Python中为自定义类实现add和iadd?

I am writing a Queue class that wraps list for most of its operations. But I do not sublcass from list, since I do not want to provide all the list API's. I have my code pasted below. The add method seems to work fine, but iadd seems to go wrong, it is printing none.

Here is the code:

import copy

from iterator import Iterator

class Abstractstruc(object):

def __init__(self):

assert False

def __str__(self):

return "" %(self.__class__.__name__,self.container)

class Queue(Abstractstruc,Iterator):

def __init__(self,value=[]):

self.container=[]

self.size=0

self.concat(value)

def add(self, data):

self.container.append(data)

def __add__(self,other):

return Queue(self.container + other.container)

def __iadd__(self,other):

for i in other.container:

self.add(i)

def remove(self):

self.container.pop(0)

def peek(self):

return self.container[0]

def __getitem__(self,index):

return self.container[index]

def __iter__(self):

return Iterator(self.container)

def concat(self,value):

for i in value:

self.add(i)

def __bool__(self):

return len(self.container)>0

def __len__(self):

return len(self.container)

def __deepcopy__(self,memo):

return Queue(copy.deepcopy(self.container,memo))

if __name__=='__main__':

q5 = Queue()

q5.add("hello")

q6 = Queue()

q6.add("world")

q5 = q5+q6

print q5

q5+=q6

print q5

Output:

None

解决方案

__iadd__ needs to return self when adding in-place:

def __iadd__(self,other):

for i in other.container:

self.add(i)

return self

__iadd__ needs to return the resulting object; for immutable types the new object, for mutable types, self. Quoting the in-place operator hooks documentation:

These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值