0 Fundamentals python-16.4 Global scope

Source: Internet
Author: User

In this chapter, we'll discuss the global scope

The essence of global: Not declaring a variable type, but declaring a namespace that is scope

1. Summary of global variables:

1) It bit with the top layer inside the module file

We open the test.py module, there is only one sentence, is the following sentence

x=1# global variables, in fact, all the functions inside the module can be used

2) do not declare or use inside the function

We'll add a little code to the test.py file.

x=1# global variables, is actually the module inside all functions can use Def test ():    print (x) if __name__== ' __main__ ':    Test ()
Output:



3) If the function is assigned a value, it must be declared

With the above code, we add a sentence that changes x in the test:

x=1# global variables, is actually the module inside all functions can use Def test ():    print (x)    x=2if __name__== ' __main__ ':        Test ()

Output:

>>> ================================ RESTART ================================>>> Traceback (most Recent:  file "c:/python34/test.py", line 6, <module>    Test ()  file "c:/python34/test.py" , line 3, in test    

It's not going to change, but we need to add the global statement.

x=1# global variables, in fact, all the functions inside the module can use Def test ():    print (x)    Global x    x=2    Print (x) if __name__== ' __main__ ':        Test ()
Output:


2. Problems with global variables

There are serious problems with using global variables, especially changing global variables, because you don't know the order of the functions that change this global variable, so debugging is cumbersome and cumbersome to use.

Let's change the code above to look like this:

x=1# global variables, is actually the module inside all functions can use Def test ():    print (x)    Global x    x=2    print (x) def test2 ():    print (x)    Global x    x= ' abc '    print (x)    if __name__== ' __main__ ':        test ()        test2 ()

Output:


Now let's change the position of some of the two functions.

x=1# global variables, is actually the module inside all functions can use Def test ():    print (x)    Global x    x=2    print (x) def test2 ():    print (x)    Global x    x= ' abc '    print (x)    if __name__== ' __main__ ':                test2 ()        test ()

Output:


Because x changes as the function executes, if there are thousands of changes in a large project, the problem is very serious.


The solution to this problem is to use a file to define all the global variables and turn the global variable into a communication tool so that it is easier to maintain

Of course, the use of global variables is not recommended under unfamiliar assumptions.


Summary: This section briefly explains the global scope and some of the issues that need to be noted in using it



This chapter is here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 Fundamentals python-16.4 Global scope

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.