First code:
# -*- coding:gb2312 -*-
nums = [11,22,33,44,55]
def nums_chang():
nums = [11,22,33,44,55,999]
print(nums)
nums_chang() # 打印出来是[11,22,33,44,55,999]
print(nums) # 打印出来是[11,22,33,44,55]
operation result:
Second code:
# -*- coding:gb2312 -*-
nums = [11,22,33,44,55]
def nums_chang():
nums.append(999) # 打印出来是[11,22,33,44,55,999]
print(nums) # 打印出来是[11,22,33,44,55,999]
nums_chang()
print(nums)
operation result:
My question is:
Why do the two results printed by the first code are different, but the results printed by the second code are the same? Especially the second piece of code, why can the append command in the function modify the global variables outside. In the first piece of code, the list is redefined, but the external global variables cannot be modified? What's the reason here?
After reading the accepted answer, I think I still missed the point.
First of all, the nums variable is defined as a global variable, which means that in this ".py" file, all classes and functions can use it. But all variables are also an object, and objects are divided into mutable and immutable. The list is a mutable object, which means it can be changed. So what are mutable objects? Those are those that can operate on objects without affecting their creation and death in memory. Enter x="234", and if you assign another value, x="111". In fact, the two x's are already two objects, they are just covered, that is, x = x + "123", and x is also a new object.
If it is a list, x = [1,2,3], now operate on it, x.remove(1), then the x object is still an x object, but the value of the object has changed
The first piece of code: It is reassignment. In different scopes, nums is assigned again, so that the global variable nums and the nums variable in the nums_chang function are no longer the same object. They are different, so they appear when you print. different values. At this time, the nums variable in the nums_chang function is already a local variable and is only used by this function
Second piece of code: nums is a list, a variable object, so operations on the list do not affect this object, so this object is still the global one, and global variables are used both in functions and outside, so after you modify it , the value of the global variable is also modified
First: Regarding the issue of scope, you can refer to one of my articles first: Python: Scope and LEGB. You can first have a basic understanding of
variables
search, and then we can see, Because there is anassignment statement
,nums
will only be searched from the local scope and will not be related to the global scopeSecond: Based on the first one, we can see that the
nums
innums.append(999)
is found from theglobal scope
, and because theappend
method is used, this method is used directly on the source list itself, so you see that the globalnums
is also updatedIf the function cannot find the variable in its own scope, it will go back to the previous scope to find it. This is how your second one came about.
If it’s the first one, it’s obvious that you have given value