Passing information between modules

Thomas Passin list1 at tompassin.net
Fri Nov 18 14:24:32 EST 2022


On 11/18/2022 10:19 AM, Tobiah wrote:
> On 11/18/22 02:53, Stefan Ram wrote:
>>    Can I use "sys.argv" to pass information between modules
>>    as follows?
>>
>>    in module A:
>>
>> import sys
>> sys.argv.append( "Hi there!" )
>>
>>    in module B:
>>
>> import sys
>> message = sys.argv[ -1 ]
> 
> Kind of seems like a code smell.  I think you would normally
> just inject the dependencies like:
> 
>      module_b.do_thing("Hi there!")
> 
> If you really want to have a module-global space,
> you could just create a module globals.py, and
> import that in every module that needs to share globals.
> You can just do globals.message = "Hi there!" and
> from another module do print globals.message.

The module can even create the message or variable dynamically.  I have 
one that, when it is loaded, asks git for the branch and changeset hash 
the code's working directory is using.  This module is imported by 
others that use the branch and changeset data in one way or another.



More information about the Python-list mailing list