난 당신이 이 할을 하려는지 볼 수 있지만, 당신은 (주로, 아래 참조) 할 수 없습니다. 코드는에 드 당을 같이 작성 : has_GroupConnection
이후
has_GroupConnection.__getitem__(i)
# foo[x] -> foo.__getitem__(x)
이 프로그램이 작동하지 않습니다에 정의되어 있지 않은 대신에 NameError
발생합니다. 당신은 그것을 정의 할 수 있습니다.
has_GroupConnection = []
has_GroupConnection.append(None) # 0th element
has_GroupConnection.append(True) # 1st element
has_GroupConnection.append(True) # 2nd element
has_GroupConnection.append(True) # 3rd element
# or indeed has_GroupConnection = [None, True, True, True]
# or a dict will work, too...
# as above...
GroupConnection = [None, 45, 88, 55]
for i in range(1,3): # note this is only [1, 2], not [1, 2, 3]
if has_GroupConnection[i]:
print(str(GroupConnection[i]))
내 첫 번째 문장, 지나친 단순화 당신 수이 eval
또는 locals()
을 사용하여이 작업을 수행하지만, 나쁜 생각, 그래서 내가 어떻게 작업을 수행하는 표시되지 않습니다 강하게 당신을 훈계합니다 그렇게하지 마라! 그것은 추한 비효율적, 그리고 끔찍한 코드 냄새 (그리고 당신은 그것을 인터넷 검색에 대해 생각을 위해 자신을 부끄러워해야!)
'GroupConnection1','GroupConnection2' 및'GroupConnection3'을 목록으로 대체하십시오. – Galen
[동적 이름을 사용하는 변수 값에 액세스] (https://stackoverflow.com/questions/29177693/access-to-value-of-variable-with-dynamic-name)의 가능한 복제본 – Barmar