global variable, ok for string, bad for int

F

francisl

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!)
 
P

Peter Hansen

francisl said:
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
 
T

Tim Roberts

francisl said:
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!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,216
Messages
2,571,120
Members
47,722
Latest member
Alexcarry541

Latest Threads

Top