1. 程式人生 > >python 在函式中訪問全域性變數

python 在函式中訪問全域性變數

python允許在函式中訪問全域性變數的值,但不允許在函式中修改全域性變數的值。

In the last video, you saw that within a function, we can print a global variable’s value successfully without an error. This worked because we were simply accessing the value of the variable. If we try to change or reassign this global variable, however, as we do in this code, we get an error. Python doesn’t allow functions to modify variables that aren’t in the function’s scope.