SoFunction
Updated on 2024-12-10

Explaining global and local variables in python

Global versus local variables in python

Tags:python

  • A local variable is a variable defined inside a function body.
  • Global variables are variables defined outside the

For example:

a = 1
def f():
    b = 2

included among theseais the global variable, andbare local variables. Local variables are only valid inside the body of the function and cannot be accessed outside of the function body, whereas global variables are valid for all of the following code.

Using global variables inside function bodies

Global variables can be used directly in the content of the function body, you can access them directly, but note that for immutable types of data, if an assignment is made inside the function, it will have no effect on the global variable outside because it is equivalent to creating a new local variable with the same name as the global one, and for mutable types, if you use an assignment statement, it will have no effect on the outside, but if you use a method, it will have an effect on the outside. For variable types, if you use an assignment statement, again there is no effect on the outside, but if you use a method, there is an effect on the outside.

As in the code below:

g_b = 3;g_l1 = [1,2];g_l2 = [1,2,3]
def t1():
    g_b = 2
    g_l1 =[]
    g_l2.append(7)
t1(g_b,g_l1,g_l2)
print(g_b,g_l1,g_l2)

global keyword

It was stated above that if an assignment statement is used, it is equivalent inside the function to thenewly builtBut sometimes we want to make the variable an external global variable, and when we assign it, we are redirecting the global variable, so we can use the global keyword to indicate that I'm using the global variable inside the function.

The method of use is as follows:

g_b = 3
def t1():
    global g_b
    g_b = 2
t1()
print(g_b)

This time you'll notice that the global variable g_b is also redirected, this is because theglobal gbmeans that it specifies the function in which theg_bIt's the one outside.

5 rules for naming python variables

Python variable names are not random, they need to follow the following rules:

1. You can only start with a letter or an underscore, not a number. Starting a variable with a lowercase letter is a Python convention and a good coding habit.

You can try your hand at which of the above 4 variable names is correct, try opening the editor to define the variable output and see what it suggests.

Of the actual 4 variable names, only the first one is correct, the next 3 are wrong. If you run it, you will get an error message similar to the one below:

2、Variable names can not have spaces, you can use underscores;

3, Python built-in keywords and functions can not be used, such as if, for, while, print, input and so on;

4. Variable names are case sensitive, for example: myname and myName are not the same variable;

5、Lower-case letter l and upper-case letter O should not be used in variable names as much as possible, as they can be easily seen as numbers 1 and 0.

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.