iadd python class

iadd in python class

我创建了一个 class 并改进了添加功能,但由于某种原因它无法正常工作。这就是我所拥有的:

def __iadd__(self,thing): 

    try: 
        tester=True
        for y in thing:
            if type(y) != type('f'):
                tester=False
        for j in thing:
            self.listt.append(j)
        return self
    except(tester==False):
        return ValueError

每当我这样做时:

f=class('ab')

f += [4]   

它应该 return 一个 ValueError 因为我添加的不是字符串,但出于某种原因它附加了 int,即使它不应该也是如此。

def __iadd__(self, thing): 
    for y in thing:
        if not isinstance(y, str):
            raise ValueError
    for j in thing:
        self.listt.append(j)
    return self