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

  • Home
  • SEARCH
  • 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 6331281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:02:01+00:00 2026-05-24T18:02:01+00:00

Since variables names are declared local if there is an assignment to them within

  • 0

Since variables names are declared local if there is an assignment to them within a function, and I want to access the module variables from within a function, can I import the module name within the module and then use that to access the module variables ?

Example (file name : server.py):

import server

bar = 5

def foo():
   server.bar = 10
  • 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-05-24T18:02:02+00:00Added an answer on May 24, 2026 at 6:02 pm

    Instead of using the global statement, as suggested in all of the other answers, don’t use module level variables, but a class as a container or else another module just for the globals:

    # mg.py
    
    bar = 5
    
    # server.py
    import mg
    
    def foo():
        mg.bar = 10
    

    or

    class mg:
        bar = 5
    
    def foo():
        mg.bar = 10
    

    This way you don’t need to put global statements everywhere, you can re-use those names, and it’s clear which bar you’re referring to.

    Edit: Also, it is possible to import a module inside itself, but you can’t change variables in the main module that way. So this would work:

    # selfimport.py
    import selfimport
    
    def foo():
        print selfimport.foo
    
    bar = 3
    
    if __name__ == '__main__':
        print selfimport.bar
        foo()
    

    But this wouldn’t:

    # selfimport.py
    import selfimport
    
    bar = 3
    
    def foo():
        selfimport.bar = 5
    
    if __name__ == '__main__':
        print selfimport.bar
        foo()
        print bar # bar is still 3!
    

    If you’re only using the globals as constants, you wouldn’t need the global statement anyway. You also need to make sure to wrap code you only want to execute in the main module in an if statement as above, or you’ll recurse.

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

Sidebar

Related Questions

Since CS3 doesn't have a web service component, as previous versions had, is there
Since Graduating from a very small school in 2006 with a badly shaped &
I am trying to 'destructure' a dictionary and associate values with variables names after
I want to call a function using a variable. Is that possible in C?
I faced one; I've set same variables names and that made me crazy, I
I have a bunch of variables that I want to check, and if they
Is there a way to get a value of a local variable specified by
How can I test wether a variable has been declared or assigned (i.e. check
I have a few questions about the linkage from the following variables. By examples
I read somewhere that interfaces can have member variables. Static final constants only, can

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.