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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:11:17+00:00 2026-06-16T23:11:17+00:00

I started to write python programs for some time, but I am not so

  • 0

I started to write python programs for some time, but I am not so good at it, at present I meet a problem, it seems very strange, at least for me.

Assume we have two source files, called A.py and B.py, their contents are as below:

A.py

import B

__global__ = 'not set yet'

class Data:
    __var__ = 'not set yet'

def init():
    global __global__
    __global__ = 'value set for global'
    Data.__var__ = 'value set for class var'

if __name__ == '__main__':
    init()

    t = B.Test()
    t.display()

    print '============================'
    print 'global(in A):    ' + __global__
    print 'class var(in A): ' + Data.__var__

B.py

import A

class Test:
    def __init__(self):
        self.glo = A.__global__
        self.var = A.Data.__var__

    def display(self):
        print 'global:          ' + self.glo
        print 'class var:       ' + self.var
        print '============================'
        print 'global(in B):    ' + A.__global__
        print 'class var(in B): ' + A.Data.__var__

then I ran python A.py, the output is as following:

global:          not set yet
class var:       not set yet
============================
global(in B):    not set yet
class var(in B): not set yet
============================
global(in A):    value set for global
class var(in A): value set for class var

In my opinion, the first 4 outputs should coincide with the last 2 outputs, but the fact is not, seems the value is set during class and method definition and cannot be changed. It is very different from many other languages, such as Java. So, could any one help explain this, or paste me some links to help understand it? And is there any workaround to solve this?

Thanks in advance,

Kelvin

==============edit==============

Thanks @icktoofay, @hop and @RocketDonkey, after I tried the code below, I do find the root cause:

import sys
import A
import __main__

__global__ = 'not set yet'

class Data:
    __var__ = 'not set yet'

def init():
    global __global__
    __global__ = 'value set for global'
    Data.__var__ = 'value set for class var'

if __name__ == '__main__':
    init()

    for key in sys.modules.keys():
        if key in ['__main__', 'A']:
            print key + ' : ' + sys.modules[key].__file__
    print '===================='
    print 'A: ' + A.__global__
    print 'A: ' + A.Data.__var__
    print '===================='
    print __main__.__global__
    print __main__.Data.__var__

the output is:

__main__ : A.py
A : D:\test\python\A.py
====================
A: not set yet
A: not set yet
====================
value set for global
value set for class var

It is because file A.py was imported twice, one is named __main__ and another is named A, the values were changed in module __main__, but for module B, the value is got from module A, so the value is not changed.

I do need to dive deeper for module importing of python. 😀

  • 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-16T23:11:19+00:00Added an answer on June 16, 2026 at 11:11 pm

    The circular imports (A imports B, B imports A) in combination with one of the modules being the main script is the problem.

    How Python imports modules

    When you ask Python to import a module, it first looks through sys.modules to see if a module by that name is already there. If so, it just uses that module. If there is no module by that name, it makes a new module object, places it in sys.modules, and runs the appropriate Python code in that module.

    Main scripts

    There’s a popular idiom that’s used in Python scripts: the ubiquitous

    if __name__ == '__main__':
    

    When Python wants to run the main script, it imports that module with the name __main__.

    That’s perfectly fine when your main script just does stuff with other modules, but when other modules want to do things to the main module again, you run into trouble: The script importing the main module again is probably importing it by a normal name (like in this case, A). Unfortunately, the already-loaded module is not named A; it’s named __main__.

    Solutions

    A very simple solution would simply be to remove the other modules’ dependencies on the main module. Then you will not encounter this unintuitive behavior. One way you might do this is have the main script just be a stub that calls out to another module’s main function or something.

    A different solution would be to have the main script alter sys.modules manually to put itself under another name. This is a little hacky, but it works:

    sys.modules['A'] = sys.modules[__name__]
    

    Now you know.

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

Sidebar

Related Questions

I am very new (started today) to writing chrome extensions but need to write
I have started to write some Python code. What I have is: from math
I started by googling and found the article How to write INSERT if NOT
Question simple and quick: I have started to use Netbeans to write some code
I'm very new to python (just started yesterday), and I'm trying to create permutations
I have started to write a Python 3.x client application. The server application exists
I am a novice to Python and have started to write my first module
I trying to write a client server application in Python, but I faced a
All, I've just started using Python (v 2.7.1) and one of my first programs
I'm just getting started with Python but already have found it much more productive

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.