对列表的修改 * 星号


for j in i:
TypeError: 'int' object is not iterable

 

l = []


def g(_l: list, i):
for j in i:
_l.append(j)


g(l, 1)
g(l, *[2, 3])


修复:

 

6. Expressions — Python 3.12.5 documentation https://docs.python.org/3/reference/expressions.html
PEP 448 – Additional Unpacking Generalizations | peps.python.org https://peps.python.org/pep-0448/






class T:
def __init__(self):
self.lst = []
# biz
for i in range(6):
self.lst.append(i)

def f(self, i):
print(i)
if i in self.lst:
self.lst.remove(i)

def __del__(self):
for i in self.lst:
self.f(i)


t = T()

 

修复:

class T:
def __init__(self):
self.lst = []
# biz
for i in range(6):
self.lst.append(i)

def f(self, i):
print(i)
if i in self.lst:
self.lst.remove(i)

#
# def __del__(self):
# for i in self.lst:
# self.f(i)

def __del__(self):
for i in [i for i in self.lst]:
self.f(i)


t = T()

 

 

 

 

posted @ 2024-06-17 12:42  papering  阅读(5)  评论(0编辑  收藏  举报