How to address a global variable in a function

Discussion in 'Python' started by n179911, Aug 8, 2009.

  1. n179911

    n179911 Guest

    HI,

    I have a global variable

    // line 8
    tx = 0

    and then I have this function (start in line 12):
    def handleTranslate(result):
    print line
    txStr, tyStr = result.group(1), result.group(2)
    print txStr, tyStr

    tx += int(txStr)
    ty += int(tyStr)

    return

    But I am getting this error:
    handleTranslate(result)
    File "buildsvg.py", line 22, in handleTranslate
    tx += int(txStr)
    UnboundLocalError: local variable 'tx' referenced before assignment

    How can I fix it?
    I have assigned 'tx' to 0 in line 8. I don't understand why i get this
    error.

    Thank you for any help.
     
    n179911, Aug 8, 2009
    #1
    1. Advertising

  2. n179911

    n179911 Guest

    On Aug 7, 8:29 pm, n179911 <> wrote:
    > HI,
    >
    > I have a global variable
    >
    > // line 8
    > tx  = 0
    >
    > and then I have this function (start in line 12):
    > def handleTranslate(result):
    >         print line
    >         txStr, tyStr = result.group(1), result.group(2)
    >         print txStr, tyStr
    >
    >         tx += int(txStr)
    >         ty += int(tyStr)
    >
    >         return
    >
    > But I am getting this error:
    >     handleTranslate(result)
    >   File "buildsvg.py", line 22, in handleTranslate
    >     tx += int(txStr)
    > UnboundLocalError: local variable 'tx' referenced before assignment
    >
    > How can I fix it?
    > I have assigned 'tx' to 0 in line 8. I don't understand why i get this
    > error.
    >
    > Thank you for any help.


    I figure out my problem

    put this in my function:
    global tx
     
    n179911, Aug 8, 2009
    #2
    1. Advertising

  3. n179911

    Chris Rebert Guest

    On Fri, Aug 7, 2009 at 11:29 PM, n179911<> wrote:
    > HI,
    >
    > I have a global variable
    >
    > // line 8
    > tx  = 0
    >
    > and then I have this function (start in line 12):
    > def handleTranslate(result):
    >        print line
    >        txStr, tyStr = result.group(1), result.group(2)
    >        print txStr, tyStr
    >
    >        tx += int(txStr)
    >        ty += int(tyStr)
    >
    >        return
    >
    > But I am getting this error:
    >    handleTranslate(result)
    >  File "buildsvg.py", line 22, in handleTranslate
    >    tx += int(txStr)
    > UnboundLocalError: local variable 'tx' referenced before assignment
    >
    > How can I fix it?
    > I have assigned 'tx' to 0 in line 8. I don't understand why i get this
    > error.


    By default, Python assumes that any variables assigned to in a
    function are local variables. To change this assumption, you need to
    declare that you want to reference a global variable. This is done by
    putting a `global` statement at the start of the function (i.e.
    `global tx`).

    In the future, you might STFW (in this case, for "UnboundLocalError")
    before posting to the newsgroup, as you would have found several
    explanations of this common newbie-biter.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Aug 8, 2009
    #3
  4. n179911

    Aahz Guest

    In article <>,
    n179911 <> wrote:
    >
    >I have a global variable
    >
    >// line 8
    >tx = 0
    >
    >and then I have this function (start in line 12):
    >def handleTranslate(result):
    > print line
    > txStr, tyStr = result.group(1), result.group(2)
    > print txStr, tyStr
    >
    > tx += int(txStr)
    > ty += int(tyStr)
    >
    > return


    BTW, you probably want to learn why global names are a bad idea, I don't
    have time to explain that here (or point you at references). Going
    through some of the online tutorials should address this.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "...string iteration isn't about treating strings as sequences of strings,
    it's about treating strings as sequences of characters. The fact that
    characters are also strings is the reason we have problems, but characters
    are strings for other good reasons." --Aahz
     
    Aahz, Aug 13, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Parkoff
    Replies:
    2
    Views:
    6,573
  2. Replies:
    1
    Views:
    405
    Michael Ekstrand
    Aug 21, 2005
  3. sairam
    Replies:
    2
    Views:
    383
    Steve Holden
    Apr 5, 2007
  4. jubelbrus
    Replies:
    5
    Views:
    642
    JohnQ
    Jul 20, 2007
  5. Replies:
    9
    Views:
    119
    Quarco
    Jul 20, 2004
Loading...

Share This Page