【发布时间】:2021-01-29 10:57:15
【问题描述】:
我目前正在学习 python。我正在学习类、继承和抽象类。这是有问题的构造函数:
def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the
contents of sourceCollection, if it's present."""
self.size = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
我收到以下错误,我不知道为什么:
TypeError: 'int' object is not iterable
如果有帮助,这是我的添加方法:
def add(self, item):
"""Adds item to self."""
# Check array memory here and increase it if necessary
self.items[len(self)] = item
self.size += 1
谁能帮助我解决我为什么会收到此错误?我做了一些研究,但无济于事。提前非常感谢!!!
【问题讨论】:
-
请始终提供minimal reproducible example。这意味着我们可以复制和粘贴它并得到相同的确切错误。如果您收到错误消息,总是发布完整的错误消息,包括堆栈跟踪
-
不要使用
self.size来跟踪列表的大小,列表比较清楚,使用len(self.items)
标签: python list class typeerror abstract-class