using 'global' across source files.

Bram Stolk bram at sara.nl
Tue Jan 2 09:33:27 EST 2001


Remco Gerlich wrote:
> 
> Bram Stolk <bram at sara.nl> wrote in comp.lang.python:
> >
> > # funcs.py
> >
> > def change_val() :
> >   global val
> >   val = val * 2
> >
> >
> >
> > # prog.py
> >
> > val = 100
> >
> > import funcs
> >
> >
> > funcs.change_val()
> > print val
> 
> You need "print funcs.val" now (just like you call the function with
> "funcs.change_val()).

Huh??
Mind you, 'val' is defined in prog.py, NOT funcs.py.
That's the whole issue I'm trying to solve.

I'm pretty confused now.
Anyway, 'print funcs.val' does not work either, 
besides the error happens BEFORE the print cmd, it
happens in the function change_val(), which cannot 
find the 'val' variable from prog.py.
Nor can it find it, if I prefix it as '__main__.val'.

> 
> Using only import means you have to give the module's name every time,
> but without that Python will never know if the value in the funcs module
> has changed.
> 
> "global" just means "global inside this module".

Well, that's my whole problem.
How do I refer to 'val' if it is in another *unnamed* module?
According to the manuals, it should simply be in the namespace
'__main__', but that doesnt work either.

   Bram
 
> --
> Remco Gerlich

-- 
------------------------------------------------------------------------------
 Bram Stolk, VR Specialist.
 SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP  AMSTERDAM
 email: bram at sara.nl   Phone +31-20-5923059  Fax +31-20-6683167

 "I heard if you play the NT-4.0-CD backwards, you get a satanic message."
 "Thats nothing, if you play it forward, it installs NT-4.0"
------------------------------------------------------------------------------



More information about the Python-list mailing list