Home  >  Q&A  >  body text

Python novice question about local variables and global variables

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?

学习ing学习ing2686 days ago1060

reply all(3)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-12 09:26:56

    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

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-12 09:26:56

    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 variablessearch, and then we can see, Because there is an assignment statement, nums will only be searched from the local scope and will not be related to the global scope

    Second: Based on the first one, we can see that the nums in nums.append(999) is found from the global scope, and because the append method is used, this method is used directly on the source list itself, so you see that the global nums is also updated

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-12 09:26:56

    If 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

    reply
    0
  • Cancelreply