Passing information between modules
Thomas Passin
list1 at tompassin.net
Sat Nov 19 16:35:15 EST 2022
On 11/19/2022 4:28 PM, Thomas Passin wrote:
> On 11/19/2022 3:46 PM, Michael F. Stemper wrote:
>> On 18/11/2022 04.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 ]
>>
>> I just tried and it appears that one can append to sys.argv. However,
>> it seems like an incredibly bad idea.
>
> For that matter, you can just directly add attributes to the sys module,
> no need to use sys.argv:
>
> >>> import sys
> >>> sys._extra = 'spam' # Not an exception
> >>> print(sys._extra)
> spam
>
> Probably not the best idea, though. Better to use some module that you
> control directly.
This could be one of those things of which Raymond Chen (The Old New
Thing) asks "what if everyone did this?". Imagine if every
(non-standard-library) module misused sys or sys.argv like this. The
result could be chaotic.
Best to put all your own stuff into modules that you yourself control.
More information about the Python-list
mailing list