【发布时间】:2015-11-17 22:31:47
【问题描述】:
我创建了一个类并改进了 add 函数,但由于某种原因它无法正常工作。这就是我所拥有的:
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]
它应该返回一个ValueError,因为我正在添加一些不是字符串的东西,但由于某种原因它正在附加 int,即使它也不应该也是如此。
【问题讨论】:
-
您使用的
try/except块错误。如果你想让它进入except块,你需要raise一个错误 -
如果您提出异常,所有
except(tester==False):会给您一个catching classes that do not inherit from BaseException is not allowed