Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:00:16+00:00 2026-06-15T22:00:16+00:00

What is a global statement ? And how is it used? I have read

  • 0

What is a global statement? And how is it used? I have read Python’s official definition;
however, it doesn’t make a lot of sense to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:00:17+00:00Added an answer on June 15, 2026 at 10:00 pm

    Every "variable" in python is limited to a certain scope. The scope of a python "file" is the module-scope. Consider the following:

    #file test.py
    myvariable = 5  # myvariable has module-level scope
    
    def func():
        x = 3       # x has "local" or function level scope.
    

    Objects with local scope die as soon as the function exits and can never be retrieved (unless you return them), but within a function, you can access variables in the module level scope (or any containing scope):

    myvariable = 5
    def func():
        print(myvariable)  # prints 5
    
    def func2():
        x = 3
        def func3():
            print(x)       # will print 3 because it picks it up from `func2`'s scope
        
        func3()
    

    However, you can’t use assignment on that reference and expect that it will be propagated to an outer scope:

    myvariable = 5
    def func():
        myvariable = 6     # creates a new "local" variable.  
                           # Doesn't affect the global version
        print(myvariable)  # prints 6
    
    func()
    print(myvariable)      # prints 5
    

    Now, we’re finally to global. The global keyword is the way that you tell python that a particular variable in your function is defined at the global (module-level) scope.

    myvariable = 5
    def func():
        global myvariable
        myvariable = 6    # changes `myvariable` at the global scope
        print(myvariable) # prints 6
    
    func()
    print(myvariable)  # prints 6 now because we were able 
                       # to modify the reference in the function
    

    In other words, you can change the value of myvariable in the module-scope from within func if you use the global keyword.


    As an aside, scopes can be nested arbitrarily deeply:

    def func1():
        x = 3
        def func2():
            print("x=",x,"func2")
            y = 4
            def func3():
                nonlocal x  # try it with nonlocal commented out as well.  See the difference.
                print("x=",x,"func3")
                print("y=",y,"func3")
                z = 5
                print("z=",z,"func3")
                x = 10
    
            func3()
    
        func2()
        print("x=",x,"func1")
    
    func1()
    

    Now in this case, none of the variables are declared at the global scope, and in python2, there is no (easy/clean) way to change the value of x in the scope of func1 from within func3. That’s why the nonlocal keyword was introduced in python3.x . nonlocal is an extension of global that allows you to modify a variable that you picked up from another scope in whatever scope it was pulled from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently making a global volunteer site, and the webpages will more or less have
I used to have a jCarousel of image that worked perfectly fine in the
Does the delete statement below doubly free an object? (...object_list is a global vector<object*>...)
So in jQuery, I have a global variable currentSubNav that stores a current visible
I'm learning objective-C and Cocoa and have come across this statement: The Cocoa frameworks
I have the following Execution statement which creates a table (using data from another
I have this function here that assembles an API call to eBay. It used
I have a custom WordPress table on a WordPress MultiSite called ihn_2_frm_item_metas used for
I have a sql statement like this (using mysql 5.5.15): select global_id, count(id) as
What does the following statement mean? Local and dynamically allocated variables have addresses that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.