The difference between a global variable and a local variable in python

Source: Internet
Author: User

The essential difference between a global variable and a local variable is the scope

To be understood in layman's words,

Global variables are declared throughout the py file and can be accessed at the global scope

A local variable is declared in a function and can be called only in that function, and if you try to call it out of range, the program explodes.

If you define a local variable with the same name as a global variable inside a function, it can cause unexpected effects, which may not be what you expect. It is therefore not recommended to use this, which makes the program very unhealthy

Take a look at a few examples to understand the difference between a global variable and a local variable:

Demo1:

  1. def fun (x):
  2. y=2
  3. Print ("Run result of multiplication:", x*y)
  4. num1=1
  5. Print ("Initial num1=", NUM1)
  6. Fun (NUM1)
  7. Print ("Y's value is:", y)

Operation Result:

The error is due to an attempt to access a local variable, but the location of the access is not in the scope of the variable y

Demo2:

  1. def fun ():
  2. num1=2
  3. print ("Inside function modified num1=", NUM1)
  4. num1=1
  5. Print ("Initial num1=", NUM1)
  6. Fun ()
  7. Print ("num1= after running function", NUM1)

Operation Result:

You can see that after modifying the global variable inside the function, the result of the modification is invalid when the function is executed, and the global variable is not affected

Look again:

DEMO3:

  1. def fun ():
  2. num1*=2
  3. print ("Inside function modified num1=", NUM1)
  4. num1=1
  5. Print ("Initial num1=", NUM1)
  6. Fun ()
  7. Print ("num1= after running function", NUM1)

Operation Result:

An error has been made. This is because the fun () function uses the local variable NUM1, which is just a local variable with the same name as the global variable, and is assigned before use, so again, don't use it.

Global keyword

If you really want to modify the value of a global variable in a function body, use the global keyword

Demo4:

  1. def fun ():
  2. Global NUM1
  3. num1=2
  4. print ("Inside function modified num1=", NUM1)
  5. num1=1
  6. Print ("Initial num1=", NUM1)
  7. Fun ()
  8. Print ("num1= after running function", NUM1)

Operation Result:

Using the Global keyword is to tell the Python compiler that the variable is not a local variable but a global variable, which is a bit like the meaning of "reference".

nonlocal keywords

Then look at another keyword associated with the variable nonlocal, which literally means that the current variable is not a local variable. Nonlocal is a new keyword in Python3.0, python2.x does not support

Let's take a look at the following code

  1. def fun ():
  2. num2=3
  3. def fun2 ():
  4. num2*=2
  5. print ("num2=", num2)
  6. return fun2 ()
  7. Fun ()

Operation Result:

The cause of the error is similar to the previous one, that is, using undefined local variables, but num2 is not a global variable, just the outer variables of the FUN2 function, forcing the use of the global definition of num2 will also be an error (do not believe you try)

This is the time to use the nonlocal keyword:  

  1. def fun ():
  2. num2=3
  3. def fun2 ():
  4. Nonlocal num2
  5. num2*=2
  6. print ("num2=", num2)
  7. return fun2 ()
  8. Fun ()

Operation Result:

This way, the program will execute normally

The difference between a global variable and a local variable in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.