473,396 Members | 2,014 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,396 software developers and data experts.

global variable, ok for string, bad for int

I have a problem when I declare global variable.
If it is string, dict, array or tuple, everything goes fine, but with int, I get an "used before declaration" error.

here a sample :

vardict = {'un':1, 'deux':2}

def print_value():
print vardict['un']
######### ok, that works

--------------------

#!/bin/python
varint = 1

def print_value():
print varint
######### ok, that failed

python 2.3.4 - windows2000

(it works in linux!)
Jul 18 '05 #1
2 1834
francisl wrote:
I have a problem when I declare global variable.
If it is string, dict, array or tuple, everything goes fine, but with
int, I get an "used before declaration" error.

here a sample :
vardict = {'un':1, 'deux':2}

def print_value():
print vardict['un']
######### ok, that works

--------------------
#!/bin/python
varint = 1

def print_value():
print varint
######### ok, that failed


Please *always* post tracebacks, the actual traceback cut and
pasted with the mouse from your session, when you are asking
for help with errors like this. There is nothing called
a "used before declaration" error... Was what you actually got
an "UnboundLocalError: local variable 'varint' referenced
before assignment", or something else? Don't retype: cut and paste.

Also, please post only the actual code you are running,
reduced to the simplest form which still reproduces the
problem. The above code runs perfectly well, even if you
add the missing call to print_value() at the end of the module.

-Peter
Jul 18 '05 #2
francisl <fr******@aei.ca> wrote:

I have a problem when I declare global variable.
If it is string, dict, array or tuple, everything goes fine, but with int, I get an "used before declaration" error.
Excuse me for being dubious, but I don't believe you. Both of the examples
you posted work fine in Python 2.3 on Windows XP. Please type the EXACT
script you ran, and show us the EXACT error message you get. You can
cut-and-paste from the cmd shell script to make sure we see the right
output.
here a sample :

vardict = {'un':1, 'deux':2}

def print_value():
print vardict['un']
######### ok, that works

--------------------

#!/bin/python
varint = 1

def print_value():
print varint
######### ok, that failed

python 2.3.4 - windows2000

(it works in linux!)

--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #3

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

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.