implementing add and iadd for custom class in python?

爷,独闯天下 提交于 2019-12-25 04:22:25

问题


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 "<%s: %s>" %(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:

<Queue: ['hello', 'world']>
None

回答1:


__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).



来源:https://stackoverflow.com/questions/20204230/implementing-add-and-iadd-for-custom-class-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!