class 의 목록 유도 식 오류
3684 단어 파 이 썬 개발
코드
class B:
b = 'b'
ls = [b + i for i in '123']
오류 알림
Traceback (most recent call last):
File "C:/Users/hongjiwei/PycharmProjects/small_draft/z.py", line 12, in <module>
class B:
File "C:/Users/hongjiwei/PycharmProjects/small_draft/z.py", line 14, in B
ls = [b + i for i in '123']
File "C:/Users/hongjiwei/PycharmProjects/small_draft/z.py", line 14, in <listcomp>
ls = [b + i for i in '123']
NameError: name 'b' is not defined
해결 방법
class A:
a = 'a'
ls = []
for i in '123':
ls.append(a + i)
print(' '.join(A.ls))
a1 a2 a3