How to make a variable's late binding crosses the module boundary?

Schachner, Joseph (US) Joseph.Schachner at Teledyne.com
Tue Aug 30 13:26:24 EDT 2022


The way we do this, is in main.py, call a "globalizer" function in each other file:

    # call globalizers to get shortcuts as global variables
    funcs.globalizer(interface, variable_dict)
    util.globalizer(interface, variable_dict)
    sd.globalizer(interface, variable_dict)
    tests.globalizer(interface, variable_dict)
    ut.globalizer(interface, variable_dict)

Obviously, you may not need a shared interface in which case you can just pass the variable dictionary.

In each file, you have a function:
def globalizer(interface, variables_dict):
    # create global variables for this .py file for shared interface and the variables
    ....

This works well, making sure separate python files shared exactly the same things we want to be global.

---- Joseph S.

Teledyne Confidential; Commercially Sensitive Business Data

-----Original Message-----
From: Stefan Ram <ram at zedat.fu-berlin.de> 
Sent: Tuesday, August 30, 2022 1:09 AM
To: python-list at python.org
Subject: Re: How to make a variable's late binding crosses the module boundary?

dn <PythonList at DancesWithMice.info> writes:
>Build module.py as:
>***
>CONSTANT = 1

>def func():
>    pass
>***

>then in the terminal:
>***
>Python 3.9.13 (main, May 18 2022, 00:00:00) [GCC 11.3.1 20220421 (Red 
>Hat 11.3.1-2)] on linux Type "help", "copyright", "credits" or 
>"license" for more information.
>>>> from module import func as f

  In CPython one then can also:

print( f.__globals__[ "CONSTANT" ])
import sys
module = sys.modules[ f.__globals__[ "__name__" ]] print( module.CONSTANT ) CONSTANT = module.CONSTANT print( CONSTANT )

  .




More information about the Python-list mailing list