473,289 Members | 1,929 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,289 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 1419

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
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.