Visibility of global variables in imported modules

前端 未结 8 1914
梦毁少年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 12:04

    Since globals are module specific, you can add the following function to all imported modules, and then use it to:

    • Add singular variables (in dictionary format) as globals for those
    • Transfer your main module globals to it .

    addglobals = lambda x: globals().update(x)

    Then all you need to pass on current globals is:

    import module

    module.addglobals(globals())

提交回复
热议问题