-
How to remove elements in a Python List while looping
a = [1, 2, 2, 3, 4] def even(x): return x % 2 == 0 for item in a: if even(item): a.remove(item) print(a)
결과: [1, 2, 3]
참고: https://www.python-engineer.com/posts/remove-elements-in-list-while-iterating/
Mutable Default Arguments
def append_to(e, to=[]): to.append(e) return to my_list = append_to(12) print(my_list) my_other_list = append_to(42) print(my_other_list)
출력결과
[12]
[12, 42]Late Binding Closure
for multiplier in [lambda x: i * x for i in range(5)]: print(multiplier(2))
출력결과
8
8
8
8
8Global Variable
x = 3 def f(): y = x + 1 x = 2 f()
결과
UnboundLocalError: local variable 'x' referenced before assignment
참고: https://python-guide-kr.readthedocs.io/ko/latest/writing/gotchas.html
'python > 응용' 카테고리의 다른 글
pydantic: Generics (0) 2023.05.28 jinja2 template 으로 Html 본문작성 후 이메일 보내기 (0) 2023.05.27 __slot__, __call__ (0) 2023.04.10 yield from (0) 2023.04.10 ABC vs Protocol (0) 2023.04.01