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 7722183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:06:22+00:00 2026-06-01T04:06:22+00:00

I have a global variables that I am using as a default variable. Depending

  • 0

I have a global variables that I am using as a default variable. Depending on what happens in my program I need the ability to change those defaults and have the changes persist through the remaining operation of my code. I want them changed and defined everywhere hence I used a global variable. Here is some test code that shows how I am trying to modify these variables.

When I do this I have the following problems…

  1. The program thinks that myGlobal hasn’t been defined in main. But
    it has. Why?
  2. When i call a subroutine after I have changed myGlobal. I didn’t want that to happen.

What is the proper way to accomplish what I am trying to do here? Examples?

#!/usr/bin/python

import sys

myGlobal = "foo"

print "********************"
print "MyGlobal %s" % myGlobal
print "********************"


def main(argv):

   #UnboundLocalError: local variable 'myGlobal' referenced before assignment
   print '1. Printing the global again: ' + myGlobal

   myGlobal = "bar"
   print "2. Change the global and print again: " + myGlobal

   # now call a subroutine
   mySub()

# Checks for output file, if it doesn't exist creates one
def mySub():
   # Why isn't the global "bar" not "foo"?
   print '3. Printing the global again: ' + myGlobal

   myGlobal = "black sheep"
   print "4. Change the global and print again: " + myGlobal




if __name__ == "__main__":
   main(sys.argv[1:])
  • 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-01T04:06:23+00:00Added an answer on June 1, 2026 at 4:06 am

    If you want to assign to, rather than read a module-global variable from within a function, you need the global keyword:

    def main(argv):
      global myGlobal
      #...
      myGlobal = "bar"
    

    Otherwise the assignment will just create a new local variable that shadows your global variable (so changes to it will not affect the global variable, like it is the case in your example).

    That said, you could also use a class here to have your functions share state in a nicer way:

    class MyApp(object):
      def main(self, argv):
        self.myVar = "bar"
        self.mySub()
    
      def mySub(self):
        print self.myVar
    
    MyApp().main(sys.argv[1:])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector declared as a global variable that I need to be
I have a big list of global variables that each have their own setup
I have some global variables in a Python script. Some functions in that script
I have a javascript function (function1) that checks some global variables (can the user
I have read in various places that having variables with global scope, i.e. a
I have a global variable that is an instance of my custom class. How
If i have global variable in A.dll, that depends on global variable in B.dll
I have a python module that makes use of a huge dictionary global variable,
I have a recursion function that parses an object/array with a global variable. If
So in jQuery, I have a global variable currentSubNav that stores a current visible

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.