ropering / Study

0 stars 0 forks source link

[Python] locals() - 변수명으로 반복문을 돌리고 싶을 때 #34

Open ropering opened 2 years ago

ropering commented 2 years ago

locals()란?


현재 문맥의 개체 매핑 뷰를 반환해요

Examples


student_1 = 'a'
student_2 = 'b'
student_3 = 'c'
student_4 = 'd'

for i in range(1, 5):
  print(locals()[f"student_{i}"])

'''result
a
b
c
d
'''