Can't see variables declared as global in a function

Jacob Davis j.foster.davis at
Wed Apr 16 09:26:29 CEST 2008


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  

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

def somefunc():
     global from_somefunc

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:

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()
 >>> anotherfunc()
 >>> isglobal
 >>> 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',  

Is there a way that I can view from_somefunc?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list