2.9 Generators #Writing Idiomatic Python 3.1

oen·2022년 7월 27일
0

1. 간단하게 한번만 반복할거면 list comprehension 대신 generator expression

list comprehension => 모든 원소가 들어있는 list

  • 크기가 큰 경우에 메모리부족

generator expression => generator

  • 필요할 때마다 각 원소를 생성

👎

for uppercase_name in [name.upper() for name in usernames]:
	print(uppercase_name)

👍

for uppercase_name in (name.upper() for name in usernames):
	print(uppercase_name)

2. 무한한 시퀀스를 lazily하게 load

무한한 시퀀스를 반복해야 할 때나 엄청 큰 크기의 시퀀스를 계산해야 할 때 사용자가 계속 기다리게 할 수는 없으니까

profile
🐾

0개의 댓글