[Tutor] Global Variable Gotcha

Kristoffer Erlandsson krier115@student.liu.se
Tue Jun 24 14:06:51 2003


On Tue, Jun 24, 2003 at 07:55:25AM -0800, Tim Johnson wrote:
> Hi All:
>     Example: I'm writting an application in python that
> consists of one module. I have a global object that I
> use in my code.
> 
> The module grows in size, and I then seperate some of
> my code into another module. I now find that when I rerun
> my reconfigured application that the code in my new namespace
> no longer recognizes the global object. I then find that
> I must rewrite code in the newly seperated functions so
> that the global object is passed as a parameter. 
> 
> I'd welcome comments on how to avoid this, and also on
> how to retain the 'visibility' of this object. I can
> offer a code example if necessary.
>  
>    Pointers/URLs to docs welcome!
> 
> Thanks.

You could use the 'from xxx import yyy' construct. If your global object
is called foo and exists in module bar you can in your additional
modules use 'from bar import foo' and it will be visible as if it was
defined in your additional modules.

Regards,
Kristoffer Erlandsson