Python basics-The use of global keywords and globals

Source: Internet
Author: User

The primary role of the global keyword in Python is to declare the scope of a variable.

In C, since variables must be declared first and then used, it is clear to us whether the variables used are global or local, such as:

int a = 5;void Test (void) {    a = 1;//not declared first, so a global variable a}void test1 (void) {    int A;    A = 2; It is declared earlier, so a local variable A is used, and the modification does not affect the global variable a}void main (void) {    printf ("before:a =%d\n", a);    Test ();    printf ("After test:a =%d\n", a);    Test1 ();    printf ("After test1:a =%d\n", a);}

In Python, variables do not need to be declared first and can be used directly, so how do we know if we are using local variables or global variables? First: The variables used by Python must be local variables by default. Second: Python requires a global prefix if you want to use a globally variable outside the scope. For example, without the global situation:
A = 5 def test ():    a = 1    print ' in test func:a =%d '% atest () print ' Global a =%d '% A
the results of the program execution are:
In Test func:a = 1Global A = 5
It can be seen that, without global, the inside of the function is not changed outside the global variables (list type exception).

The following is the case with the global prefix:

A = 5def test (): Global    A #此处声明, tell the execution engine: I'm going to use global variable A, don't make it part of the whole!    a = 1    print ' in test func:a =%d '% atest () print ' Global a =%d '% A

Execution Result:

In Test func:a = 1Global A = 1
as you can see, the value of the global variable has been successfully modified inside the function.

In fact, many articles on the web are advocating another way to use global variables: Using a separate global file.

The method is as follows: 1. Under the same folder, create a new 2 files: myglobal.py test.py2.The global variables are placed in myglobal.py, with the following examples:
A = 0B = 1c = 2d = 3
3. test.py is the test code, which can use global variables
Import Myglobaldef Test ():    myglobal.a = 100print ' myglobal a =%d '% myglobal.atest () print ' After test, myglobal a =% d '% MYGLOBAL.A
the results of the execution of test.py are as follows:
Myglobal a = 0after test, myglobal a = 100
OK, also successfully modified the global variable (this statement is not accurate, but let's call it).

In practice, both approaches have advantages, and most of the time we just write small-function scripts in Python, which is enough with the global keyword. If you write a large functional application, the latter method can be used to make Management of global variablesMore convenient.

Python basics-The use of global keywords and globals

Related Article

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.