Passing information between modules

Avi Gross avi.e.gross at gmail.com
Sun Nov 20 10:29:44 EST 2022


There is no guarantee that argv is consulted earlier in the program than
other modules will use it for communication.

Consider a case where a program does look at argv but later wants to call
another program using some or all of the components of argv and now there
are added components there. That could lead to all kinds of problems.

Some languages have global objects available that you can add to, sort of
like a dictionary object and as long as you add keys guaranteed  to be
unique,  can be used carefully to coordinate between parts of your program.
Of course, you may also need to use techniques that ensure atomic
concurrency.

Reusing argv is a hack that should not be needed.


On Sat, Nov 19, 2022, 4:37 PM Thomas Passin <list1 at tompassin.net> wrote:

> 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.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list