SoFunction
Updated on 2024-10-28

Python Local Variables Global Explained

I. Scope

Local:local scope
Enclosing:nested scope (computing)
nonlocal : only works on nested scopes and only inside functions
global :global scope
Built-in:built-in scope

The order in which python variables are used:Current-scope local variables - > outer-scope variables - > global variables in the current module - > python built-in variables.

Variable scopes:

When you declare, change, or find a variable name in a Python program, you do it in a namespace that holds the variable name, which is also called the variable's scope. python's scope is static, and the location in the code where the variable name is assigned determines the extent to which the variable can be accessed. The scope of a Python variable is determined by the location of the variable in the source code.

In general, variables defined outside the function body become global variables, and variables defined inside the function are called local variables.

Local variables are available in all scopes, and local variables are only available in this function.

The order in which variables are used is local > global variables, i.e.: local variables are used first.

So the question is, if you want to use a global variable within a function, or change the value of a global variable, what should you do?

The global keyword appears:

To solve the problem of using global variables within functions, python adds the global keyword, which can be used to specify the scope of a variable.

What the global keyword does: Declares that the variable var is global.

II. Local scopes

Local variables are defined without the use of the keyword, and are local by default when they are defined.

In the following code, c = 100, c is a local variable. c is defined inside the function and is not available outside the function.

def dummy():
i = 0
c = 100
print(i)
i += 1
print(c)

III. Global scopes

To use the keyword global, you have to first qualify it with the global keyword and then assign the value

def dummy():
i = 0
global c
c = 100
print(i)
i += 1
print(c)

IV. Nonlocal scopes

nonlocal is generally used in closures, where the variables declared in nonlocal are in the parent local scope, not globally defined.

i = 0
n = 0
def dummy1():
i = 1
print(i)
def dummy2():
i = 2
dummy2()
print(i)
dummy1()
print(i)

Output results:

If the variable declared in it does not exist in the parent localization, an error is reported

1. Use nonlocal

i = 0
def dummy1():
i = 1
print(i)
def dummy2():
nonlocal i # Non-local, only applies inside the closure
i = 2
dummy2()
print(i)
dummy1()
print(i)

2. Principle of proximity

b = 1111
def func5():
b = 5
print('l29', b)

def func6():
b = 6
print('l33', b) # Here's b from the inside out in close proximity #

func6()
print('l36', b)
func5()

Output results:

3, function execution order

Although this does not report an error, the runtime will report an error

a = 123
def func1():
print(a)
print(b)
func1() # Can't print b
b = 456
func1() # Printableb

Output results: Unless the call to func1 on line 32 is placed after line 33, it will not report an error.

4, loops, judgment code blocks in the scope of the problem

There are no scopes like this in the loop code block

if True:
a = 100
print(a)

for i in range(1, 5):
print(i)
print(i)

Output results:

This article on the Python local variable global detailed article is introduced to this, more related Python global content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!