Home > database >  python global variable function in function
python global variable function in function

Time:09-15

Good afternoon, I will try to explain everything not with words but with examples

As we know, to use the variable X in this case, we need to update it via global

enter image description here

but what if the variable X is declared in a function and we want to get it from another function

enter image description here

If I use global, I get an error

enter image description here

CodePudding user response:

Use nonlocal:

The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope excluding globals.

  • Related