Python: Modify Global list inside a function

后端 未结 5 1761
耶瑟儿~
耶瑟儿~ 2020-11-30 09:21

First of all, I understand that I can use global statement to access global variables. But somehow I was able to modify a global list without global

5条回答
  •  醉话见心
    2020-11-30 10:08

    • 2 variables nums are different and they point to a same object or 2 different objects, though they have same name.
    • when you call func1(nums), mean that you pass a reference. Now the 2 variable nums point to same object. (2 variables, 1 object)
    • when you assign in func1, the inside variable nums will point to a new object, the outside is still unchanged (2 variables, 2 object)
    • and when you call print nums then this nums is the outside variable,

提交回复
热议问题