Modify global variables python

for example. External function defines the count variable, and then to use it to modify the function in the interior.
We can see, we can print it, but can not modify it.
Here Insert Picture DescriptionHere Insert Picture Description

the reason

python immutable variables (such as str, int, double, tuple these), if declared outside the function, then the internal use only can not be modified, if you want to modify only the outside of the stated time plus global keyword in use it also must first explain:

while True:
    try:
        n = input()
        global count
        count =1
        def inner():
        #说明使用的count变量为全局的不是局部的
            global  count
            print(count)
            count += 1
            print(count)
        inner()
    except:
        break

The variable variable (such as list, dict) python stated in the outer internal functions can be modified.

Guess you like

Origin blog.csdn.net/dpengwang/article/details/92951242