我看到这段代码有奇怪的行为:
images = dict(cover=[],second_row=[],additional_rows=[])
for pic in pictures:
if len(images['cover']) == 0:
images['cover'] = pic.path_thumb_l
elif len(images['second_row']) < 3:
images['second_row'].append(pic.path_thumb_m)
else:
images['additional_rows'].append(pic.path_thumb_s)
我的web2py应用程序出现以下错误:
if len(images['cover']) == 0: TypeError: object of type 'NoneType' has no len()
我想不出这里面有什么问题。可能是范围问题?
第一次分配:
images['cover'] = pic.path_thumb_l
时,它将最初存储在images['cover']
中的空列表的值替换为pic.path_thumb_l
的值,即None
。也许这一行的代码必须是
images['cover'].append(pic.path_thumb_l)
您为
images['cover']
分配了新内容:其中
pic.path_thumb_l
在代码中的某个时刻是None
。你可能想加上:
你的问题是
if len(images['cover']) == 0:
检查图像值的长度[“cover”]要做的是检查它是否有值。
请改为:
if not images['cover']:
相关问题 更多 >
编程相关推荐