global variable, ok for string, bad for int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • francisl

    #1

    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!)
  • Peter Hansen

    #2
    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 "UnboundLocalEr ror: 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

    Comment

    • Tim Roberts

      #3
      Re: global variable, ok for string, bad for int

      francisl <francisl@aei.c a> 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.

      Comment

      Working...