【发布时间】:2015-09-10 10:28:22
【问题描述】:
为什么我可以删除列表中的字符串但不能删除类?如果列表中有课程,可以这样做吗?这样做的正确方法是什么?
class Temp():
def __init__(self, name, name2):
self.n = name
self.n2 = name2
cltemp1 = Temp("1", "2")
cltemp2 = Temp("3", "4")
x = ["1", "2"]
clx = [cltemp1, cltemp2]
def remove_string_from_class():
global x
for x[:] in x:
del x[0]
remove_string_from_class()
print x
def remove_class_from_list():
global clx
for clx[:] in clx:
del clx[0]
remove_class_from_list()
print clx
TypeError:只能分配一个可迭代对象
【问题讨论】:
-
for clx[:] in clx和for x[:] in x是什么意思? -
与早期函数中的 x[:] 相同。我想要它的意思是:对于列表 clx 中的每个元素,删除此列表中的第一个元素,直到它为空。
-
你可能得试试
for c in clx[:]: -
while clx也可以。我很好奇,for x[:] in x是从哪里来的?它看起来确实适用于字符串列表,但我以前没见过。
标签: python class python-2.7 python-3.x del