Visibility of global variables in imported modules

前端 未结 8 1842
梦毁少年i
梦毁少年i 2020-11-22 11:22

I\'ve run into a bit of a wall importing modules in a Python script. I\'ll do my best to describe the error, why I run into it, and why I\'m tying this particular approach t

8条回答
  •  孤独总比滥情好
    2020-11-22 11:41

    This post is just an observation for Python behaviour I encountered. Maybe the advices you read above don't work for you if you made the same thing I did below.

    Namely, I have a module which contains global/shared variables (as suggested above):

    #sharedstuff.py
    
    globaltimes_randomnode=[]
    globalist_randomnode=[]
    

    Then I had the main module which imports the shared stuff with:

    import sharedstuff as shared
    

    and some other modules that actually populated these arrays. These are called by the main module. When exiting these other modules I can clearly see that the arrays are populated. But when reading them back in the main module, they were empty. This was rather strange for me (well, I am new to Python). However, when I change the way I import the sharedstuff.py in the main module to:

    from globals import *
    

    it worked (the arrays were populated).

    Just sayin'

提交回复
热议问题