0
비슷한 질문이 있습니다. Python variable scope error. 불변 변수와 관련이 있습니다. 하지만 mutable 변수를 테스트 할 때 파이썬 인터프리터가 변수의 범위를 결정하는 방법을 모르겠습니다. 변경 가능한 대python 변수 범위가
def test_immutable():
a = 1
b = 2
def _test():
print(a)
print(b)
a += 1
print(a)
_test()
def test_mutable():
_dict = {}
def _test():
print(_test.__dict__)
_dict['name'] = 'flyer'
print('in _test: {0}'.format(_dict['name']))
_test()
print(_dict['name'])
if __name__ == '__main__':
# test_immutable() # throw exception
test_mutable() # it's ok
귀하의 질문은 귀하가 링크 한 것과 동일합니다. 하나의 경우에는 변수를 할당하고, 다른 경우에는 참조하는 * 경우에 해당합니다. – Bakuriu