in-module global variables

Hakan Nilsson hakan at netg.se
Thu Sep 28 12:29:31 CEST 2000


Just use the 'global' keyword, adding the line

    global myglobalvar

to your function, before you use the variable.

/Hakan


On Thu, 28 Sep 2000, Matthew Banham wrote:

> Dear python users,
> 
> When writing a python module, how do I correctly declare a global variable-
> i.e. a variable that has scope throughout the whole of the module?
> 
> For example, if I have a module called mymod.py with the following in it:-
> 
> myglobalvar = 10
> 
> def myfunc(somparm):
>     #here there are some commands
>     print myglobalvar
> 
> when I call myfunc from within the module, myglobalvar is not recognised
> giving the "Unbound Local Error"
> 
> Can anyone help?
> 
> Yours Sincerely,
> Matthew Banham
> 
> ----------------------------------------------------------------------------
> ----
> Matthew Banham, MSci
> Development Engineer & Technical Support Officer
> 
> Photon Design                Tel:  +44 1865 395480
> 68 Church Way                Fax: +44 1865 395481
> Iffley, Oxford               Email: matb at photond.com
> OX4 4EF                      Web: www.photond.com
> 
> 

-
 Hi! I'm a .signature virus!
 Copy me into your .signature file to help me spread!





More information about the Python-list mailing list