Can't see variables declared as global in a function

Gary Herron gherron at islandtraining.com
Wed Apr 16 04:38:18 EDT 2008


Jacob Davis wrote:
> Hi.
>
> I have a hundred lines of code in a module that declare some global 
> variables inside a function so that those variables can be used by 
> other functions.  I want to import this module so that I can more 
> easily debug by looking at the value of individual variables.  But 
> when I try to reach these variables, I get a warning that they are not 
> defined.
>
> I am on an Intel Mac running Leopard 10.5.2, Python 2.5
>
> Here is an example of some code that has the same problem:
>
>
>
>
> #!/usr/bin/env python
>
> global isglobal
> isglobal=200
>
> def somefunc():
>     global from_somefunc
>     from_somefunc=5
>
> def anotherfunc():
>     return from_somefunc+30
>
>
>
>
> So during debugging I want to look at the variable from_somefunc
>
> here is my terminal output. I start by looking at dir(), then 
> run somefunc(), then run anotherfunc(), then I want to look 
> at from_somefunc but I get a warning:

Yuck, YUCK, YUCK!   You are breaking *so* many 
good-programming-practices, I hardly know where to start.

First off:  A python global is not what you think.  There are *no* 
program wide globals.  There are only module wide globals.  Also, the 
"global isglobal" is absolutely meaningless as anything declared there 
is a (module level) global by definition.   

SO...

Your from_somefunc is a global variable in module test_vars, but you are 
trying to access it from your main module (the interactive session).  
The fact that you did "from test_vars import *" isn't going to fix this, 
because at the time you did the import, from_somefunc was not defined ad 
so was not imported by the "*".

You could just "import test_vars", and then after calling your function 
test_vars.somefunc(), you would find that test_vars.from_somefunc would 
be defined.





BUT...  DON'T DO THAT!   A better way:   (It is still slightly dubious, 
but it is much more straightforward, and does not use the global statement.)



Define your vars.py module:

  isglobal=200
  # and nothing else

And from your main program,

  import vars
  print vars.isglobal   # will print 200
  vars.from_somefunc = 5    # will define and set a global in vars
  print vars.from_somefunc+30   # retrieves a global value from vars



As I said above this is still slightly dubious.  Better solutions would 
probably involve creating a class with each of your huindred variables 
as attributes of the class, or a class, each instance of which has the 
hundred variables, or ... whatever.   To give you better advice, we'd 
have to know more about what problem you are trying to solve.


If you really getters and setters (that's what we might call somefucn 
and anotherfunc) then you really should be using a class to contain all 
the hundred variables, and define getter/setter methods for them.


Gary Herron





>
>
>
> Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
> [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from test_vars import *
> >>> dir()
> ['__builtins__', '__doc__', '__name__', 'anotherfunc', 'isglobal', 
> 'somefunc']
> >>> somefunc()
> >>> anotherfunc()
> 35
> >>> isglobal
> 200
> >>> from_somefunc
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'from_somefunc' is not defined
> >>> dir()
> ['__builtins__', '__doc__', '__name__', 'anotherfunc', 'isglobal', 
> 'somefunc']
>
>
>
> Is there a way that I can view from_somefunc? 
>
> Thanks,
>
> Jake
>




More information about the Python-list mailing list