如何解决如何将项目正确添加到列表
我目前正在学习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()
):
def add(self,item):
"""Adds item to self."""
# Check array memory here and increase it if necessary
self.items.append(item)
self.size += 1
,
getDataFromFile()
具有一种方法,可以添加所有可迭代元素
不要使用list
来跟踪self.size
中的元素数量,请使用self.items
len(self.items)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。