또 다른 관련 질문은 "How do I pass a variable by reference?"입니다. Daren Thomas는 변수 패스가 파이썬에서 어떻게 작동하는지 설명하기 위해 과제를 사용했습니다. append 메소드의 경우, 우리는 비슷한 방식으로 생각할 수 있습니다."list_of_variables"의 1, 2 항목이 연결되어있어
list_of_variables[0] = list_of_values --> [1, 2, 3]
list_of_values --> [1, 2, 3, 10]
list_of_variables[1] = list_of_values --> [1, 2, 3, 10]
:로
list_of_variables = []
list_of_values = [1, 2, 3]
list_of_variables.append(list_of_values)
print "List of variables after 1st appending: ", list_of_variables
list_of_values.append(10)
list_of_variables.append(list_of_values)
print "List of variables after 2nd appending: ", list_of_variables
첨부하는 작업이 생각 될 수있다, 당신은 "list_of_variables"목록에 목록 "LIST_OF_VALUES"를 추가한다고 가정 해 봅시다 메모리에서 동일한 객체에 상기의 출력은 : 한편
List of variabiles after 1st appending: [[1, 2, 3]]
List of variables after 2nd appending: [[1, 2, 3, 10], [1, 2, 3, 10]]
목록 "만일 _of_values "는 변수이므로 동작이 달라집니다.
list_of_variables = []
variable = 3
list_of_variables.append(variable)
print "List of variabiles after 1st appending: ", list_of_variables
variable = 10
list_of_variables.append(variable)
print "List of variables after 2nd appending: ", list_of_variables
첨부 된 동작은 이제 등가이다
list_of_variables[0] = variable --> 3
variable --> 4
list_of_variables[1] = variable --> 4
출력은 다음 변수 LIST_OF_VALUES 사이
List of variabiles after 1st appending: [3]
List of variables after 2nd appending: [3, 10]
차이는 제자리 나중에 한 변화이다.
'조건'과 'm'의 의미는 무엇입니까? 'fi' 란 무엇입니까? 'f() '란 무엇입니까? –
X는 함수를 기반으로 업데이트됩니다. m은 for 루프의 범위입니다. while 루프는 임의의 수의 단계에 대해 실행됩니다. – abcd
하지만 이제는 혼란스러워서, 당신이'f1'과'fi'를 가지고 있음을 알았습니다. 그건 당신의 코드에서 오자입니까? – zenpoy