473,413 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Re: Can't see variables declared as global in a function

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
Jun 27 '08 #1
0 1425

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: gabor | last post by:
hi, as far as i know in python there aren't any private (i mean not accessible from the outside of the object) methods/fields. why? in java/c++ i can make a method private, this way...
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
35
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
2
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
11
by: sinbad | last post by:
hi, I've a very large C program consisting of hundred of files; I want to know what are all the global variables defined in the program. how do i do this. thanks
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.