코드의 몇 가지 문제점에는 변수 이름이 포함됩니다. 그것은 당신을 혼란스럽게 할 수 있습니다.
class test:
# I guess you want to provide the name to initialize the object attribute
def __init__(self, name):
# self.name is the attribute where the name is stored.
# I prefer it to self.A
self.name = name
이제 문제는 인스턴스가 목록의 요소이기도합니다. 나는 이름입니다.
if __name__=='__main__':
# I presume these are list of names
list_of_names = ['A','b','c']
# You have to store your instance some where.
instance_list = []
# Here name is an element of the list that you are iterating
# I change it to name instead of instance
for name in list_of_names:
# Here I am appending to the list, a test object that I create
instance_list.append(test(name))
[편집 :] :
for item in list:
item=class() # How can you reassign the item ?
이 항목이 무엇인지 참조 코드의이 작품이 왜
지금, 내가 정말 당신을 이해하지 않습니다.
>>> for item in ['A', 'B']:
... print item
...
A
B
>>>
당신은 그것을 item = ....
를 할당하지 말아야하지만 당신은 그것을 .... = ..(item)
를 사용해야합니다!
"for i in seq :"를 호출하면 i는 seq의 원본 개체가 아니라 해당 개체의 복사본입니다. 따라서 코드가 작동하지 않습니다. – Kabie
왜 이걸 시도해? 'class '가 함수로 사용 된 곳은 어디입니까? 파이썬을 배우기 위해 당신이 따르고있는 튜토리얼은 무엇입니까? –
죄송합니다, 새로운 파이썬과 새로운 stackoverflow 있습니다. 내 투쟁을 용서해주십시오. – Todd