Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 09:29 PM
francisl
Guest
 
Posts: n/a
Default 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!)
  #2  
Old July 18th, 2005, 09:29 PM
Peter Hansen
Guest
 
Posts: n/a
Default Re: global variable, ok for string, bad for int

francisl wrote:[color=blue]
> 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[/color]

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
  #3  
Old July 18th, 2005, 09:30 PM
Tim Roberts
Guest
 
Posts: n/a
Default Re: global variable, ok for string, bad for int

francisl <francisl@aei.ca> wrote:[color=blue]
>
>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.[/color]

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.
[color=blue]
>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!)[/color]
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles