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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:25:36+00:00 2026-06-14T11:25:36+00:00

Code I’d like to use a global variable in other modules with having changes

  • 0

Code

I’d like to use a global variable in other modules with having changes to its value “propagated” to the other modules.

a.py:

x="fail"
def changeX():
    global x
    x="ok"

b.py:

from a import x, changeX
changeX()
print x

If I run b.py, I’d want it to print “ok”, but it really prints “fail”.

Questions

  1. Why is that?
  2. How can I make it print “ok” instead?

(Running python-2.7)

  • 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-14T11:25:37+00:00Added an answer on June 14, 2026 at 11:25 am

    In short: you can’t make it print “ok” without modifying the code.

    from a import x, changeX is equivalent to:

    import a
    x = a.x
    changeX = a.changeX
    

    In other words, from a import x doesn’t create an x that indirects to a.x, it creates a new global variable x in the b module with the current value of a.x. From that it follows that later changes to a.x do not affect b.x.

    To make your code work as intended, simply change the code in b.py to import a:

    import a
    a.changeX()
    print a.x
    

    You will have less cluttered imports, easier to read code (because it’s clear what identifier comes from where without looking at the list of imports), less problems with circular imports (because not all identifiers are needed at once), and a better chance for tools like reload to work.

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

Sidebar

Related Questions

Code: <?php //initializing script, do not modify session_start(); define('IN_SCRIPT', true); //so that global.php cannot
code: class Node: def __init__(self, key, children=[]): self.key = key self.children = children def
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Code: # it's an ajax request, so parameters are passed via GET method def
Code: <%=#{time_ago_in_words(comment.created_at)} ago %> What i'd like is for it not to have ABOUT
code below. i'm tryind to obtain string answers like a1, c4 this is what
Code: digits.each do |digit| puts digit.content #&0000000000032056.00000032 056 value = digit.content[24..-1].strip puts value #32
Code is like this $.get( //'index.php?route=sale/order/infoforhome&token=<?php echo $token; ?>', 'index.php?route=common/home/orderinfo&token=<?php echo $token; ?>', {
Code I have: cell_val = CStr(Nz(fld.value, )) Dim iter As Long For iter =
Code that is untestable really annoys me. The following things make oo-code untestable: global

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.