我目前正在学习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
有人能帮我解释为什么我会犯这个错误吗?我做了一些研究,但没有结果。提前谢谢你
只要做(
.append()
):list
有一个方法可以从iterable中添加所有元素不要使用
self.size
来跟踪self.items
中的元素数量,请使用len(self.items)
相关问题 更多 >
编程相关推荐